How to let the chrome forget the page scroll posit

2020-08-13 02:20发布

For instance, I open a web page, and scroll down to some position,

then I refresh the chrome browser, the browser could scroll the previous position again

How can I use javascript or css to let the browser to forget the scroll?

I have try $(window).scrollTop(0), but it doesn't work

3条回答
2楼-- · 2020-08-13 02:27

It is resolved in the question below.

Disable brower's auto scroll after a page refresh?

// bypass auto scrolling.
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual';
}
查看更多
一纸荒年 Trace。
3楼-- · 2020-08-13 02:31

Here is a clean way of getting this done.

window.addEventListener('unload', function(e){
   document.body.style.display = 'none';
});

By simply setting the body display to 'none' you don't have to worry about a flash of the browser scrolling to the top of the page before it is unloaded and the scroll position will automatically be reset to 0.

查看更多
劳资没心,怎么记你
4楼-- · 2020-08-13 02:46

A simple way that I can say is to run this code on page load:

document.location = "#";

OR

window.scrollTo(0);

It set the browser viewport to the top of the page, again.

查看更多
登录 后发表回答