首页 > 其他分享 >刷新页面不记住滚动位置

刷新页面不记住滚动位置

时间:2022-08-20 09:34:26浏览次数:89  
标签:滚动 位置 scrollRestoration 刷新 页面 history

在浏览器的默认行为中,会默认记住滚动位置,刷新后会回到之前的滚动位置,在一些场景中,会希望每次刷新都回到页面的最顶端。下面介绍下实现的方法。

history.scrollRestorationAPI

MDN:https://developer.mozilla.org/zh-CN/docs/Web/API/History/scrollRestoration

语法

const scrollRestore = history.scrollRestoration
Copy to Clipboard

auto将恢复用户已滚动到的页面上的位置。manual未还原页上的位置。用户必须手动滚动到该位置。

只需要将下面的代码插入页面代码中即可

if(history.scrollRestoration){
	history.scrollRestoration = "manual"
}

标签:滚动,位置,scrollRestoration,刷新,页面,history
From: https://www.cnblogs.com/wanna2leo/p/16607159.html

相关文章