天天看點

Angularjs 回到頂部指令

<!DOCTYPE html>

<html >

  <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" target="_blank" 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>

繼續閱讀