Chrome remembers scroll position

妖精的绣舞 提交于 2019-11-26 19:51:30

问题


I'm running into a problem that's actually a "feature" on Chrome. As most of you might know, Chrome remembers a scroll position that it returns to, whenever you come back to a page. And I kind of have a problem with that.

Is there any way to override this without the user noticing?

Mees

Failed try-outs:

  • ScrollTop on document.ready

回答1:


I've checked on chrome, it worked well. Sometimes setTimeout does trick :)

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



回答2:


In Chrome 46+, the auto scroll behavior can be turned off using history.scrollRestoration:

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

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




回答3:


x = 0;  //horizontal coord
y = document.height; //vertical coord
window.scroll(x,y);

Some Javascript like that may very well be able to be manipulated to stop the auto scrolling.

It depends though, are you happy for the scroll to be simply set to automatically go to the top, or are you actually looking for the Chrome standard option to take the page to last scroll position, to be turned off completely?

What are you currently attempting to use for scrollTop()?




回答4:


I solved this by attaching to scroll event, and then resetting scroll position the first time a user scrolls. Works for on-spot reloads for me.

Looks like this:

var scrollResetOnce = false;
$(window).on("scroll", function() {
    if (scrollResetOnce) return;
    scrollResetOnce = true;
    scrollTo(0, -1);
});



回答5:


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.



来源:https://stackoverflow.com/questions/16239520/chrome-remembers-scroll-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!