Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Tell me please, how can I disable auto-upto top of my page? If I use hash nav:

<a href="#/index">Goto index</a>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
424 views
Welcome To Ask or Share your Answers For Others

1 Answer

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...

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...