Force page scroll position to top at page refresh in HTML

前端 未结 13 1249
野的像风
野的像风 2020-12-02 07:57

I am building a website which I am publishing with divs. When I refresh the page after it was scrolled to position X, then the page is loaded with the scroll po

13条回答
  •  佛祖请我去吃肉
    2020-12-02 08:41

    To reset window scroll back to top, $(window).scrollTop(0) in the beforeunload event does the tricks, however, I tested in Chrome 80 it will go back to the old location after the reload.

    To prevent that, set the history.scrollRestoration to "manual".

    //Reset scroll top
    
    history.scrollRestoration = "manual";
    
    $(window).on('beforeunload', function(){
          $(window).scrollTop(0);
    });
    

提交回复
热议问题