更改路线不会滚动到新页面的顶部

编程入门 行业动态 更新时间:2024-10-03 21:24:56
本文介绍了更改路线不会滚动到新页面的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我发现了一些不受欢迎的行为,至少对我来说,当路线改变时.在教程的第 11 步 http://angular.github.io/angular-phonecat/step-11/app/#/phones您可以看到电话列表.如果您滚动到底部并点击最新的一个,您会看到滚动条不在顶部,而是在中间.

我也在我的一个应用程序中发现了这个,我想知道如何让它滚动到顶部.我可以手动完成,但我认为应该有其他优雅的方式来做到这一点,我不知道.

那么,有没有一种优雅的方式在路线改变时滚动到顶部?

解决方案

问题在于您的 ngView 在加载新视图时保留了滚动位置.您可以指示 $anchorScroll 在视图更新后滚动视口"(docs 有点含糊,但在此处滚动意味着滚动到新视图的顶部).

解决方案是将 autoscroll="true" 添加到您的 ngView 元素:

I've found some undesired, at least for me, behaviour when the route changes. In the step 11 of the tutorial http://angular.github.io/angular-phonecat/step-11/app/#/phones you can see the list of phones. If you scroll to the bottom and click on one of the latest, you can see that the scroll isn't at top, instead is kind of in the middle.

I've found this in one of my apps too and I was wondering how can I get this to scroll to the top. I can do it mannually, but I think that there should be other elegant way to do this which I don't know.

So, is there an elegant way to scroll to the top when the route changes?

解决方案

The problem is that your ngView retains the scroll position when it loads a new view. You can instruct $anchorScroll to "scroll the viewport after the view is updated" (the docs are a bit vague, but scrolling here means scrolling to the top of the new view).

The solution is to add autoscroll="true" to your ngView element:

<div class="ng-view" autoscroll="true"></div>

这篇关于更改路线不会滚动到新页面的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 17:25:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:到新   路线   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!