使用ui-router和Angularjs自动滚动到TOP [英] Autoscroll to TOP with ui-router and Angularjs
问题描述
我已经阅读了很多不同的问题,并且没有一个解决方案似乎适合我的用例。我开始只是在我的所有链接上放置target =_ top,但这实际上迫使我的应用重新加载哪些不起作用。我也看到有人说他们使用 autoscroll =true,但只有在我的 ui-view 中才会有效。
I've read so many different problems with this and none of the solution given seem to fit my use case. I started by simply putting target="_top" on all my links, but that actually forces my app to reload which wont work. I've also seen people say they use autoscroll="true" but that only seems to work if its within my ui-view.
这个问题是在我的index.html文件中我修复了导航和其他静态元素,这些元素位于我的第一个ui-view之上。这意味着当我转到其他页面时,当页面加载超过这些元素时,我将丢失导航。我也试过把它放在身上:
The issue with this is that in my index.html file I have fixed nav and other static elements that are above my first ui-view. This means when I go to other pages I lose the navigation as the page loads past those elements. I've also tried putting this on the body with:
<body autoscroll="true">
</body>
这似乎也没有做任何事情。所以问题是,如何确保新页面(ui-router的新路由更改)从页面顶部开始?谢谢!
This doesn't seem to do anything either. So the question is, how can I make sure that new pages (new route changes from ui-router) result in starting at the top of the page? THANKS!
推荐答案
如果您希望它始终滚动到0跨浏览器,请不要对自动滚动执行任何操作。只需将其设置为运行块:
If you want it to always scroll to 0 cross-browser, do nothing with autoscroll. Just place this your run block:
$rootScope.$on('$stateChangeSuccess', function() {
document.body.scrollTop = document.documentElement.scrollTop = 0;
});
这篇关于使用ui-router和Angularjs自动滚动到TOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!