JavaScript:使用后退按钮时,防止浏览器恢复滚动位置 [英] JavaScript: Prevent browser from restoring scroll positions when using the back button

查看:67
本文介绍了JavaScript:使用后退按钮时,防止浏览器恢复滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向我的网站添加了一些页面转换.现在,只要有人单击浏览器的后退"按钮,即会将其重定向到以前的站点,他在该页面上的滚动位置就会自动恢复.如果用户在上一页上向下滚动,则此行为会导致当前窗口中的丑陋页面跳转在消失前不到一秒钟.

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)我可以延迟此默认浏览器行为吗?

1) Am I able to delay this default browser behavior?

2)如果不可能为1,我是否可以禁用默认行为并存储&而是手动恢复滚动位置?

2) If 1 is not possible, am I able to disable the default behavior and store & restore scroll positions manually instead?

推荐答案

尽管是实验性的,但您可以尝试调整" manual".

Although it is experimental, you can try adjusting the History scrollRestoration from "auto" to "manual".

这里有一些可以进行填充来帮助跨浏览器兼容也是.

There are a few polyfills out there to help with cross-browser compatability too.

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

这篇关于JavaScript:使用后退按钮时,防止浏览器恢复滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆