angularjs锚点的使用

有很多人在使用ng-view时都用到了#号做route,所以如果在页面上需要用到锚点的时候就会比较头疼了。这个时候可以使用ng的anchorScroll,也可以使用jsGen中封装的anchorScroll。推荐使用jsGen中的anchorScroll。

services.js

.factory('anchorScroll', function () { function toView(element, top, height) { var winHeight = $(window).height(); element = $(element); height = height > 0 ? height : winHeight / 10; $('html, body').animate({ scrollTop: top ? (element.offset().top - height) : (element.offset().top + element.outerHeight() + height - winHeight) }, { duration: 200, easing: 'linear', complete: function () { if (!inView(element)) { element[0].scrollIntoView( !! top); } } }); } function inView(element) { element = $(element); var win = $(window), winHeight = win.height(), eleTop = element.offset().top, eleHeight = element.outerHeight(), viewTop = win.scrollTop(), viewBottom = viewTop + winHeight; function isInView(middle) { return middle > viewTop && middle < viewBottom; } if (isInView(eleTop + (eleHeight > winHeight ? winHeight : eleHeight) / 2)) { 

转载于:https://www.cnblogs.com/Shirley-He/p/5564178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值