$anchorScroll angular锚点服务

angular锚点服务 $anchorScroll

 

普通的html页面中,我们会通过在url后面添加#elementId的方式,将页面显示定位到某个元素上,也就是所谓的锚点。 

但是在angular应用的页面上,页面路由的写法是#route/route,锚点会被当做一个页面路由解析过去,达不到定位的目的。angular提供了$anchorScroll用来提供锚点的功能。

用法:  $anchorScroll([hash])

当被调用的时候,页面会滚动到与元素相关联的指定的hash处,或者滚动到当前$location.hsh()处。

<div ng-controller="ScrollController">

  <a ng-click="gotoBottom()">Go to bottom</a>
  <a id="bottom"></a> You're at the bottom!

</div>
angular.module('anchorScrollExample', [])

.controller('ScrollController', ['$scope', '$location', '$anchorScroll',
  function ($scope, $location, $anchorScroll) {

    $scope.gotoBottom = function() {

      // 将location.hash的值设置为
      // 你想要滚动到的元素的id
      $location.hash('bottom');

      // 调用 $anchorScroll()
      $anchorScroll();

    };
  }]);

angular还提供了一种方式,用来获取路由 #后面的地址,用法:

$scope.$id;

 

jQuery也提供了锚点服务:

$('body,html').animate({ scrollTop: $('#bottom').offset().top }, 500);

 

转载于:https://www.cnblogs.com/lyy-2016/p/9003807.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值