如何实现页面刷新后不定位到之前的滚动位置?

1.scrollTop

scrollTop 为 0

2.history.scrollRestoration

使用很简单,在页面的任意位置执行下面几行 JS 代码就可以了:

if (history.scrollRestoration) {
  history.scrollRestoration = 'manual';
}

语法和兼容性

history.scrollRestoration 支持下面两个属性值:

auto
默认值,表示滚动位置会被存储。
manual
单词的意思就是手动。表示,滚动的位置不会被存储。

兼容性

 

posted @ 2024-03-22 17:49  SimoonJia  阅读(33)  评论(0编辑  收藏  举报