Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retain the scroll position of a scrollable area when pressing back button?

Tags:

I have a long list of links inside a big scrollable div. Each time when a user click on a link then click the back button, it starts at the very top of the div. It is not user friendly to our users. Any ways to let the browser scroll to the previous position when pressing the back button?

Thank you very much!

like image 743
user2335065 Avatar asked Mar 23 '15 04:03

user2335065


People also ask

How can I retain the scroll position of a scrollable area when pressing back button in react?

You can try $("div#element"). load(function() {}) and place the element outside the document ready handler. Also, if a user scroll down the page, then leave the page, and go back to the page again, it should be a new start for him so it should start at the top. But now it also remembers the previous position.

How do you preserve the scroll position?

Preserve scroll position allows you to maintain the same scroll location when you transition between two screens. This applies to scroll depth in a vertical scroll, as well as the user's location inside a horizontal scrolling element. Figma automatically preserves your scroll position for Smart Animate.

Does Chrome remember scroll position?

Bookmark this question. Show activity on this post. 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.


3 Answers

During page unload, get the scroll position and store it in local storage. Then during page load, check local storage and set that scroll position. Assuming you have a div element with id element. In case it's for the page, please change the selector :)

$(function() {    $(window).unload(function() {       var scrollPosition = $("div#element").scrollTop();       localStorage.setItem("scrollPosition", scrollPosition);    });    if(localStorage.scrollPosition) {       $("div#element").scrollTop(localStorage.getItem("scrollPosition"));    } }); 
like image 197
mohamedrias Avatar answered Sep 25 '22 16:09

mohamedrias


I think we should save scroll data per page, also we should use session storage instead of local storage since session storge effects only the current tab while local storage shared between all tabs and windows of the same origin

$(function () {
            var pathName = document.location.pathname;
            window.onbeforeunload = function () {
                var scrollPosition = $(document).scrollTop();
                sessionStorage.setItem("scrollPosition_" + pathName, scrollPosition.toString());
            }
            if (sessionStorage["scrollPosition_" + pathName]) {
                $(document).scrollTop(sessionStorage.getItem("scrollPosition_" + pathName));
            }
        });
like image 37
Jondi Avatar answered Sep 25 '22 16:09

Jondi


I had the same problem with a simple user interface consisting of a fixed menu div and a scrolling document div ("pxeMainDiv" in the code example below). The following solution worked for me in Chrome 47.0.2526.106 m and in Firefox 43.0.3. (My application is for use in-house and I did not need to cater for old versions of IE).

$(document).ready(function(){
    if (history.state) {
       $("#pxeMainDiv").scrollTop(history.state.data);
    }
    $("#pxeMainDiv").scroll(function() {
       var scrollPos = $("#pxeMainDiv").scrollTop();
       var stateObj = { data: scrollPos };
       history.replaceState(stateObj, "");
    });
});

On the div scroll event, the scroll position of the div is stored in the state object inside the browser history object. Following a press of the Back button, on the document ready event, the scroll position of the div is restored to the value retrieved from the history.state object.

This solution should work for the reverse navigation of an arbitrarily long chain of links.

Documentation here: https://developer.mozilla.org/en-US/docs/Web/API/History_API

like image 38
G Stokes Avatar answered Sep 25 '22 16:09

G Stokes