简体   繁体   English

JavaScript:使用后退按钮时,防止浏览器恢复滚动位置

[英]JavaScript: Prevent browser from restoring scroll positions when using the back button

I added some page transitions to my site. 我向我的网站添加了一些页面转换。 Now whenever someone clicks on the browser back button, that redirects him to his previous site, his scroll position on that page gets restored automatically. 现在,只要有人单击浏览器的“后退”按钮,即会将其重定向到以前的站点,他在该页面上的滚动位置就会自动恢复。 If the user has scrolled down on the previous page, this behavior leads to an ugly page jump in the current window less than a second before it fades out. 如果用户向下滚动到上一页,此行为将导致当前窗口在淡出前不到一秒钟的时间内跳转到丑陋的页面。

1) Am I able to delay this default browser behavior? 1)我可以延迟此默认浏览器行为吗?

2) If 1 is not possible, am I able to disable the default behavior and store & restore scroll positions manually instead? 2)如果不可能为1,我是否可以禁用默认行为并改为手动存储和恢复滚动位置?

Although it is experimental, you can try adjusting the History scrollRestoration from "auto" to "manual" . 尽管这是实验性的,但您可以尝试将“ 历史记录”滚动恢复从"auto""manual"

There are a few polyfills out there to help with cross-browser compatability too. 也有一些polyfill可以帮助跨浏览器兼容。

 if ('scrollRestoration' in history) { // Back off, browser, I got this... history.scrollRestoration = 'manual'; } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM