您好,登錄后才能下訂單哦!
本文實例講述了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> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> <h2>hello world</h2> </body> </html>
tmpl.html
<div id="gotoTop" title="返回頂部"> <div class="arrow"></div> <div class="stick"></div> </div>
更多關于AngularJS相關內容感興趣的讀者可查看本站專題:《AngularJS指令操作技巧總結》、《AngularJS入門與進階教程》及《AngularJS MVC架構總結》
希望本文所述對大家AngularJS程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。