Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring page scroll position with jQuery

Tags:

jquery

People also ask

How to set page scroll position in jQuery?

To set scrolltop position in jQuery, use the scrollTop() method. The scrollTop( ) method gets the scroll top offset of the first matched element. This method works for both visible and hidden elements.

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

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.


We had the exact same problem, and the following code works great...

var scroll = $(window).scrollTop();
// yada
$("html").scrollTop(scroll);

var position= $(window).scrollTop();

//some things here

$(window).scrollTop(position);

It worked for me in both IE8 and FF.


Be sure to use return false; to terminate your function(){} construct.

The event trigger may be trying to execute the default action of the target DOM element i.e., < a href="" >;


Are u use aspx? if it's aspx:

$(document).ready(function () {
        //------------ scroll-------------
         
        $('body').scrollTop(document.getElementById('<%=hdScroll.ClientID%>').value);
            
        $(window).scroll(function () {
            $("#<%= hdScroll.ClientID %>").val($(window).scrollTop());
            //alert(document.getElementById('<%=hdScroll.ClientID%>').value);
        });
       });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <asp:HiddenField Id="hdScroll" runat="server"></asp:HiddenField>