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);

随机推荐

  1. 浅析NSTextContainer

    浅析NSTextContainer TextKit中的NSTextContainer有点晦涩难懂,如果想用TextKit实现文本分页的效果,你是必须要使用NSTextContainer的...... ...

  2. 使用 JSONModel

    Magical Data Modelling Framework for JSON https://github.com/icanzilb/JSONModel New: In version 0.12 ...

  3. ZT pthread_cleanup_push()/pthread_cleanup_pop()的详解

    pthread_cleanup_push()/pthread_cleanup_pop()的详解 分类: Linux 2010-09-28 16:02 1271人阅读 评论(1) 收藏 举报 async ...

  4. [2018HN省队集训D1T1] Tree

    [2018HN省队集训D1T1] Tree 题意 给定一棵带点权树, 要求支持下面三种操作: 1 root 将 root 设为根. 2 u v d 将以 \(\operatorname{LCA} (u ...

  5. php 实现hash表

    hash表又称散列表,通过把关键字key经过hash函数映射到hash表中某个位置获取记录. 存放记录的数组又称为hash表,映射函数称为hash函数 下面是php中实现hash表的方法 <?p ...

  6. MySQL-5.6版本GTID的主从复制

    mysql GTID Replication 一.GTID的概述: 1.全局事物标识:global transaction identifieds. 2.GTID事物是全局唯一性的,且一个事务对应一个 ...

  7. 经验总结13--EF配置

    EF配置,开发前的准备及步骤. 使用V22013和EF6.1. 1.使用VS新建MVC项目. 2.创建实体类. 3.配置web.config的数据库链接字符串. <connectionStrin ...

  8. ethereumjs/ethereumjs-common-2-API文档

    https://github.com/ethereumjs/ethereumjs-common/blob/master/docs/index.md 该API的调用的详细例子可见ethereumjs/e ...

  9. 横向滑动页面,导航条滑动居中的 js 实现思路

    最近在做新闻咨询页的项目,各个新闻频道通过横向滑动切换,顶部的导航active栏需要跟着切换到对应频道,并且active到达中部时,要一直处在中间. 类似效果就是uc浏览器<UC头条>的导 ...

  10. PAT乙级1002

    1002 写出这个数 (20 分)   读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 ...