AngularJS实现的锚点楼层跳转功能示例

2019-11-16,,,

本文实例讲述了AngularJS实现的锚点楼层跳转功能。分享给大家供大家参考,具体如下:

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <meta charset="UTF-8">
    <title>anchor</title>
    <style>
      #wei div{
        width:100%;
        height:720px;
        background: red;
        color:#fff;
        text-align:center;
        line-height: 720px;
        margin:20px;
        font-size: 80px;
      }
      #wei ul{
        position: fixed;
        top:300px;
        right:60px;
      }
      #wei ul li{
        width:20px;
        display:block;
        height:20px;
        background: gray;
        color:#fff;
        text-align:center;
        line-height: 20px;
        border-radius: 50%;
        margin-bottom: 20px;
        cursor: pointer;
      }
    </style>
  </head>
  <body ng-controller="show">
      <div id="wei">
        <div ng-repeat="attr in arr" ng-attr-id="div{{attr}}">{{attr}}</div>
        <ul><!-- 定义右边的点 -->
          <li ng-repeat="attr in arr" ng-click="jump('div'+attr)">{{attr}}</li>
        </ul>
      </div>
    <script src="angular.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
      var app = angular.module('myApp',[]);
      app.controller('show',['$scope','$location','$anchorScroll',function($scope,$location,$anchorScroll){
        $scope.arr=[1,2,3,4,5];
        $scope.jump=function(id){
          //console.log(id);
          $location.hash(id);//添加锚点
          $anchorScroll();  //重新定义服务,解决当滑动时点击锚点无作用的bug
        }
      }]);
    </script>
  </body>
</html>

运行效果如下:

点击锚点2:

点击锚点3:

更多关于AngularJS相关内容感兴趣的读者可查看本站专题:《AngularJS指令操作技巧总结》、《AngularJS入门与进阶教程》及《AngularJS MVC架构总结》

希望本文所述对大家AngularJS程序设计有所帮助。

您可能感兴趣的文章:

  • 基于AngularJS的拖拽文件上传的实例代码
  • 使用angular帮你实现拖拽的示例
  • angular-ui-sortable实现可拖拽排序列表
  • Angular实现的敏感文字自动过滤与提示功能示例
  • Angular实现点击按钮控制隐藏和显示功能示例
  • Angular实现的简单定时器功能示例
  • Angular实现的日程表功能【可添加及隐藏显示内容】
  • AngularJS实现的根据数量与单价计算总价功能示例
  • Angular实现较为复杂的表格过滤,删除功能示例
  • AngularJS实现的简单拖拽功能示例

《AngularJS实现的锚点楼层跳转功能示例.doc》

下载本文的Word格式文档,以方便收藏与打印。