Take User Back to Where They Scrolled to on previous page when clicking Browser Back Button

后端 未结 6 1134
半阙折子戏
半阙折子戏 2020-12-05 01:14

Is it possible to take a user back to the area of a page where they scrolled down to when pressing the back button in a browser? As in --- pageA is double your screen size

6条回答
  •  失恋的感觉
    2020-12-05 01:46

    You can do this by using session storage.

    $(window).scroll(function () {
      //set scroll position in session storage
      sessionStorage.scrollPos = $(window).scrollTop();
    });
    var init = function () {
       //return scroll position in session storage
       $(window).scrollTop(sessionStorage.scrollPos || 0)
    };
    window.onload = init;
    

提交回复
热议问题