AngularJS.如何禁用自动滚动到我的页面顶部 [英] AngularJS. How to disable auto-scroll to top of my page
问题描述
请告诉我,如何禁用自动升至页面顶部?如果我使用哈希导航:
Tell me please, how can I disable auto-upto top of my page? If I use hash nav:
<a href="#/index">Goto index</a>
我的页面没有达到顶部,但如果我使用 AngularJS:html:
my page doest up to top, but if I use AngularJS: Html:
<a ng-href="#/index">Goto index</a>
JS:
$routeProvider.
when('/index', {
template:'...',
controller: 'RouteCtrl'
})
我的页面滚动到顶部.如何禁用它?
my page scrolled to top. How can I disable it?
推荐答案
我觉得你的普通 href
不滚动而 ng-href
滚动有点奇怪,我还以为是反过来...
I find it a bit strange that your plain href
doesn't scroll and ng-href
scrolls, I thought it was the other way round...
但是,对于解决方案;滚动哈希更改取决于浏览器,因此通常要禁用它,您需要拦截和 preventDefault()
事件,然后自己更改位置.使用 Angular 时,您可以为 <a>
元素定义一些属性指令,也可以只定义自己的 a
指令.
But, to the solution; It's up to the browser to scroll on hash changes, so usually to disable it you need to intercept and preventDefault()
the event and then change the location by yourself. When using Angular, you can either define some attribute directive for the <a>
element or just define your own a
directive.
如果你使用 ng-view
,它依赖于带有视图更新的 $anchorScroll
服务来模拟浏览器通常会做的行为,因为 Angular 已经拦截了事件.您可以通过提供自己的 $anchorScroll
来防止视图加载滚动:
If you use ng-view
, it relies on $anchorScroll
service with view updates to simulate the behaviour what the browser would normally do, as Angular already intercepts the event. You can prevent the scroll on view load by providing your own $anchorScroll
which does nothing:
angular.module('yourModule', []).value('$anchorScroll', angular.noop);
这篇关于AngularJS.如何禁用自动滚动到我的页面顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!