user3051233
user3051233

Reputation: 33

Prevent scroll jump during refreshing a long HTML page

On refreshing a long HTML page, the scroll position is initialized to the top and then jumped to the last scroll position.

Is there a way to stop this scroll jump behavior on refresh and just initialize scroll position to last scroll position?

Upvotes: 3

Views: 2683

Answers (3)

Paul van Eijden
Paul van Eijden

Reputation: 11

Isn't this just default browser behavior? What you're describing sounds like all mayor browsers are already doing it like that.

Upvotes: 0

Tom Chung
Tom Chung

Reputation: 1422

How about use html5 localStorage function.

window.addEventListener('scroll', function () {
    localStorage.scrollX = window.scrollX;
    localStorage.scrollY = window.scrollY;
})
window.addEventListener('load',function () {
    window.scrollTo(localStorage.scrollX || 0, localStorage.scrollY || 0);
})

Check it on http://jsfiddle.net/g5NKG/10/show/

Upvotes: 6

Pippin
Pippin

Reputation: 1086

It sounds like you may need the .scrollTop() method from jQuery found here

So:

$(document).ready(function(){
    $(this).scrollTop(0);
});

From link in comments, this also is a quick fix:

$(document).scrollTop(0);

Upvotes: 0

Related Questions