Jump to content

Maintain Scroll Position


The Little Guy

Recommended Posts

OK, I want to maintain scroll position, I found this:

 

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}
           
function setScrollXY(x, y) {
    window.scrollTo(x, y);
}

 

But, the problem is that I have no idea how to use it...

 

I found it here: http://abeautifulsite.net/notebook/10

Link to comment
Share on other sites

It's as simple as it looks, however it may need to be updated in order to still be cross browser compatible.

 

You use the top function to get the position of the browsers scrollbar and store that value somewhere.  I assume that for most sites, it will be [0,n] since not many sites scroll horizontally.

 

From there, say you stored that value in a cookie, if the user came back to that page, you could read that value and apply the second function to scroll the browser window back to that spot.

 

 

pseudocode:

(ex: user is reading a long post)

 

user enters page

  has user been here before

    [yes] - is value stored?

        [y] - scroll to

        [n] - do nothing

    [no] - do nothin

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.