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

后端 未结 6 1128
半阙折子戏
半阙折子戏 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:28

    You can use the following code to detect the scroll position.

     $(window).scroll(function (event){
        var scroll = $(window).scrollTop();
        // Do something
     });
    

    Then store scroll in a session and when you click back do scrollTop(scroll) .

    0 讨论(0)
  • 2020-12-05 01:29

    After trying @bowen option I have arrived to a better one with smooth scroll and without the problem said by @Frits

          $(window).scroll(function () {
            //set scroll position in session storage
            if ($(window).scrollTop() > 500)
              sessionStorage.scrollPos = $(window).scrollTop();
          });
    
          var init = setTimeout(function(){
             //return scroll position in session storage
             if (sessionStorage.scrollPos > 500){
                $("html").animate({ scrollTop: sessionStorage.scrollPos },2000);
              }
            },1000);
          window.onload = init;
    
    0 讨论(0)
  • 2020-12-05 01:40
      //For Angular
      //Save your scroll position once you scroll
      @HostListener('window:scroll', ['$event']) onScrollEvent($event){
        sessionStorage.scrollPos = window.scrollY
      } 
    
      //Scroll to the save value postion
      ngAfterViewInit(){
        window.scrollTo(0, sessionStorage.scrollPos)
      }
    
    0 讨论(0)
  • 2020-12-05 01:40
    <button onclick="goBack()">Go Back</button>
    
    <script>
    function goBack() {
      window.history.back();
    }
    </script>
    

    This will work same as the back button on browser

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-12-05 01:48

    If the content is loaded after page "load" event firing, then the back button would not take you back to the position you were. Because the browser scrolls before the 'load' event.

    To make the browser remember the scroll position in this case, you have to store the scroll position and status (what content have been loaded) somewhere before navigating away. Either in the cookie, or in the url hash.

    If pageA is just a static page without dynamic content (loaded after 'load' event, the browser should remember the scroll position when you go back.

    For dynamic content, there at least includes two parts. One is recovering the page status when click "Back" button, so all the dynamic content is loaded, some expander are expanded or collapsed. The other is scroll to there.

    The first part depends on how the page is implemented. The 2nd part you can put the scroll top into the cookie when page doing onUnload. For example

    $(window).unload(function() {$.cookie('scrollTop',$(window).scrollTop());});
    
    0 讨论(0)
提交回复
热议问题