AngularJS实现的回到顶部指令功能实例
程序员文章站
2022-03-20 23:18:52
本文实例讲述了angularjs实现的回到顶部指令功能。分享给大家供大家参考,具体如下:
本文实例讲述了angularjs实现的回到顶部指令功能。分享给大家供大家参考,具体如下:
<!doctype html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="bootstrap.min.css" rel="external nofollow" > <script src="jquery.min.js"></script> <script src="angular.js"></script> <script src="bootstrap.min.js"></script> <style type="text/css"> #gototop { display: none; position: fixed; top: 80%; left: 90%; cursor: pointer; padding: 4px 4px; width: 50px; border: 1px solid #e0e0e0; background: #fff; border-radius: 50px; height: 50px; } #gototop.hover { background: #e0e0e0; color: #fff; text-decoration: none; } .arrow { width: 0; height: 0; margin: 0 auto; border: 9px solid transparent; border-bottom-color: #aaa; } .stick { width: 8px; height: 14px; margin: 0 auto; border-radius: 1px; background-color: #aaa; } </style> <script type="text/javascript"> var myapp = angular.module('myapp', []); myapp.directive("gototop", function() { return { restrict: 'e', replace: true, scope: { minheight : '@' }, templateurl : 'tmpl.html', link: function(scope, elem, attrs) { elem.click(function() { jquery('html,body').animate({scrolltop:0}, 700); }) .hover(function() { jquery(this).addclass("hover"); }, function() { jquery(this).removeclass("hover"); }); scope.minheight = scope.minheight ? scope.minheight : 600; jquery(window).scroll(function() { var s = jquery(window).scrolltop(); if( s > scope.minheight) { jquery("#gototop").fadein(100); } else { jquery("#gototop").fadeout(200); }; }); } }; }); </script> </head> <body ng-app="myapp"> <go-to-top min-height="800"></go-to-top> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> <h1>hello world</h1> </body> </html>
tmpl.html
<div id="gototop" title="返回顶部"> <div class="arrow"></div> <div class="stick"></div> </div>
更多关于angularjs相关内容感兴趣的读者可查看本站专题:《angularjs指令操作技巧总结》、《angularjs入门与进阶教程》及《angularjs mvc架构总结》
希望本文所述对大家angularjs程序设计有所帮助。