问题描述
请告诉我,我怎么可以禁用自动高达我的网页顶部?
如果我使用哈希导航:
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()
的事件,然后自行更改位置。当使用角度,你可以定义一些属性指令中的&LT; A&GT;
元素,或者只是定义自己的 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-视图
,它依赖于 $ anchorScroll
服务与视图更新模拟行为是什么浏览器通常会做,因为已经折角拦截事件。您可以通过提供prevent上查看负载滚动自己的 $ 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。如何禁用自动滚动到我的页面顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!