Chrome remembers scroll position

2019-01-14 02:05发布

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

5条回答
唯我独甜
2楼-- · 2019-01-14 02:39

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>
查看更多
叼着烟拽天下
3楼-- · 2019-01-14 02:55

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);
});
查看更多
beautiful°
4楼-- · 2019-01-14 02:58

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

查看更多
Bombasti
5楼-- · 2019-01-14 03:02

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.

查看更多
地球回转人心会变
6楼-- · 2019-01-14 03:04
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()?

查看更多
登录 后发表回答