简体   繁体   English

防止浏览器在按下后退按钮时捕捉到上一个滚动位置

[英]Prevent browser from snapping to previous scroll position when pushing back button

I need to prevent browser from snapping to previous scroll position when the user pushed a back button like this:当用户按下后退按钮时,我需要防止浏览器捕捉到上一个滚动位置:

<a href="javascript:history.go(-1)"
      onClick="javascript:history.go(-1)" title="< GO BACK"></a>

after pushing that button the browser will return to the previous scroll position on this page i want to stop that behavior and just load the top of the page.按下该按钮后,浏览器将返回到此页面上的前一个滚动位置,我想停止该行为并只加载页面顶部。

Hope someone know a proper solution.希望有人知道正确的解决方案。

-exemple- -例如-

open a page scroll down go to a new page and hit the back button the page will auto scroll down to the place you scrolled before!打开一个页面向下滚动转到一个新页面并点击后退按钮页面将自动向下滚动到您之前滚动的位置!

I'm fairly certain the behavior you're describing is is best classified as one of those things considered to be a user preference.我相当肯定您所描述的行为最好归类为被认为是用户偏好的事物之一。 (One of those things that you're not meant to tamper with) (你不想篡改的那些东西之一)

@mrtsherman came up with a hack/workaround for this , but unless it's seriously breaking your webapp's usability, I think you should let the browser behave as the user would normally expect it to behave (and scroll to the position they were in when they left the page). @mrtsherman 为此提出了一个黑客/解决方法,但除非它严重破坏了您的 web 应用程序的可用性,否则我认为您应该让浏览器按照用户通常期望的方式运行(并滚动到他们离开时所处的位置这一页)。 Be sure to upvote mrtsherman for his sweet nugget of js if you use it.如果您使用它,请务必为 mrtsherman 的 js 甜蜜金块点赞。

I have the same problem and I haven't find yet the correct solution,我有同样的问题,我还没有找到正确的解决方案,
but I can give you a very good workaround for this problem.但是我可以为您提供解决此问题的非常好的方法。

Just scroll to the top before load the next page.加载下一页之前只需滚动到顶部。
When the user click back, the browser will scroll to the top.当用户单击返回时,浏览器将滚动到顶部。

Example:例子:

First Page:第一页:
<a href="page2.htm" onclick="window.scrollTo(0, 0);">Next</a>

Second Page:第二页:
<a href="page1.htm" onclick="history.go(-1); return false;">Back</a>

Note that href="page1.htm" loads only if user select open in new window/tab请注意href="page1.htm"仅在用户选择在新窗口/选项卡中打开时加载

Excuse my English.原谅我的英语。 I hope this helps.我希望这有帮助。

I found a better solution!我找到了更好的解决方案!

Put the following code on every page:在每一页上放置以下代码:

<script>
  setTimeout('window.scrollTo(0, 0);', 1);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止浏览器捕捉到后退按钮上的上一个滚动位置 - Prevent browser from snapping to previous scroll position on back button JavaScript:使用后退按钮时,防止浏览器恢复滚动位置 - JavaScript: Prevent browser from restoring scroll positions when using the back button 防止转到浏览器后退按钮上的上一页 - Prevent going to the previous page on browser back button 防止浏览器后退按钮不转到上一页 - Prevent browser back button to not go to previous page 移动设备上的 Safari 浏览器后退按钮,不会将用户返回到上一页的最后滚动位置 - Safari browser back button on mobile, doesn't return user to last scroll position on previous page Javascript-单击后退按钮时,将页面滚动到上一页的最后位置 - Javascript - Scroll page to last position of previous page when the back button is hit 单击返回按钮时如何记住上一页的滚动 position - how to remember previous page's scroll position when click back button 防止浏览器的后退按钮重新打开上一页的弹出窗口 - Prevent browser's back button from reopening pop up window on previous page 滚动到div位置,除非按下浏览器后退按钮 - Scroll to div position unless browser back button is pressed 防止浏览器后退按钮 - Prevent browser back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM