Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remember scroll position of page

Tags:

html

jquery

php

I am submitting some data to my database then reloading the same page as the user was just on, I was wondering if there is a way to remember the scroll position the user was just on?

like image 733
HurkNburkS Avatar asked Oct 05 '12 10:10

HurkNburkS


People also ask

How do I know my scroll position?

To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.

How do you maintain the scroll position?

To make sure a scrolling Artboard stays in position when you click on a prototype Link, select the Link you're working with and enable the Maintain scroll position after click option in the PROTOTYPE tab of the Inspector.

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.

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.


2 Answers

I realized that I had missed the important part of submitting, so, I decided to tweak the code to store the cookie on click event instead of the original way of storing it while scrolling.


Here's a jquery way of doing it:

jsfiddle ( Just add /show at the end of the url if you want to view it outside the frames )

Very importantly, you'll need the jquery cookie plugin.

jQuery:

// When document is ready...
$(document).ready(function() {

    // If cookie is set, scroll to the position saved in the cookie.
    if ( $.cookie("scroll") !== null ) {
        $(document).scrollTop( $.cookie("scroll") );
    }

    // When a button is clicked...
    $('#submit').on("click", function() {

        // Set a cookie that holds the scroll position.
        $.cookie("scroll", $(document).scrollTop() );

    });

});


Here's still the code from the original answer:

jsfiddle

jQuery:

// When document is ready...
$(document).ready(function() {

    // If cookie is set, scroll to the position saved in the cookie.
    if ( $.cookie("scroll") !== null ) {
        $(document).scrollTop( $.cookie("scroll") );
    }

    // When scrolling happens....
    $(window).on("scroll", function() {

        // Set a cookie that holds the scroll position.
        $.cookie("scroll", $(document).scrollTop() );

    });

});

@Cody's answer reminded me of something important.

I only made it to check and scroll to the position vertically.

like image 107
Joonas Avatar answered Sep 29 '22 06:09

Joonas


(1) Solution 1:

First, get the scroll position by JavaScript when clicking the submit button.

Second, include this scroll position value in the data submitted to PHP page.

Third, PHP code should write back this value into generated HTML as a JS variable:

<script>
var Scroll_Pos = <?php echo $Scroll_Pos; ?>;
</script>

Fourth, use JS to scroll to position specified by the JS variable 'Scroll_Pos'

(2) Solution 2:

Save the position in cookie, then use JS to scroll to the saved position when page reloaded.

like image 28
jondinham Avatar answered Sep 29 '22 08:09

jondinham