Chrome会记住滚动位置

ame*_*_me 25 html javascript scroll google-chrome viewport

我遇到的问题实际上是Chrome上的"功能".正如大多数人可能知道的那样,只要您回到页面,Chrome就会记住它返回的滚动位置.我有点问题.

有没有办法在没有用户注意的情况下覆盖它?

米斯

试用失败:

  • 在document.ready上的ScrollTop

小智 58

在Chrome 46+中,可以使用history.scrollRestoration关闭自动滚动行为:

if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual';
}
Run Code Online (Sandbox Code Playgroud)

来源:https://developers.google.com/web/updates/2015/09/history-api-scroll-restoration

  • 这个答案需要更多的欣赏!GJ Cory,谢谢. (2认同)

Ikr*_*rom 10

我检查过铬,效果很好.有时setTimeout会欺骗:)

<script type="text/javascript">
window.onload=function(){
    setTimeout(function(){
        scrollTo(0,-1);
    },0);
}
</script>
Run Code Online (Sandbox Code Playgroud)

  • 对于现代浏览器来说,修改`scrollRestoration`的答案是更好的 (4认同)
  • 实际上,将超时设置为0并不会使函数立即运行 - 而是将函数添加到当前执行队列的末尾(滚动到记住位置的代码将在此队列中) (3认同)
  • 这有效,但只要再次开始滚动,页面就会跳转到记忆位置 (2认同)