Prevent Refresh 'Jump' with Javascript

后端 未结 5 816
滥情空心
滥情空心 2021-02-05 17:20

I\'ve noticed that if you are on a page and you have scrolled down a good bit, if you refresh the page, most browsers will jump you back down to your position. Is there any way

5条回答
  •  后悔当初
    2021-02-05 18:12

    On Chrome, even if you force scrollTop to 0 it will jump afterwards after the first scroll event.

    You should bind the scroll to this:

    $(window).on('beforeunload', function() {
        $(window).scrollTop(0);
    });
    

    So the browser is tricked to believe that it was on the beginning before the refresh.

提交回复
热议问题