user123_456
user123_456

Reputation: 5805

how to save the scroll state

I would like to save/determine the scroll state(current position) so when the user comes back to the previous site he can continue his work from the position where he left it.

Can I continuously save the scroll position to the global variable and read it when user comes back?

Upvotes: 0

Views: 923

Answers (2)

ntgCleaner
ntgCleaner

Reputation: 5985

using javascript, you can find out the scroll height then set that into a cookie. Not a great way to do it, but it's possible.

finding the scroll height:

$(document).ready(function(){
    $(window).scroll(function(){
       var scrolled = $(this).scrollTop(); 
        console.log(scrolled);
    });
});

I would suggest using the jquery cookie plugin to set it into a cookie or session: http://archive.plugins.jquery.com/project/Cookie http://www.sitepoint.com/eat-those-cookies-with-jquery/

then add the variable to the cookie:

$(document).ready(function(){
    $(window).scroll(function(){
        $.removeCookie("mySiteScrolled");
        var scrolled = $(this).scrollTop(); 
        $.cookie("mySiteScrolled", scrolled);
        console.log(scrolled);
    });
});

Then add the "Check for scrolled" statement

$(document).ready(function(){
    var scrolled = $.cookie("mySiteScrolled");
    if(scrolled){
        $(window).scrollTop(scrolled);
    }
    $(window).scroll(function(){
        $.removeCookie("mySiteScrolled");
        var scrolled = $(this).scrollTop(); 
        $.cookie("mySiteScrolled", scrolled);
        console.log(scrolled);
    });
});

Upvotes: 4

KJ Price
KJ Price

Reputation: 5994

I would probably use jQuery to easily determine the scoll position $(document).scrollTop(); and use local storage to set/get the position.

Upvotes: 0

Related Questions