开发者

Restoring page scroll position with jQuery

I have a page operation that uses something like:

$('#thetable tbody').replaceWith(newtbody);

in an ajax callback. Sometimes, if the user had scrolled the page down, this operation has the understandable side effect of scrolling the page back up. But the replacement appears seamless to the user so it's a bit annoying to have to scroll back down again. And since the newtbody norm开发者_运维技巧ally has the same vertical height as the one it replaced, we should be able to make the script do it instead.

Now, since I found that executing:

$('body').scrollTop(300);

from the JS debugger console does what I hoped it would, I thought the simple remedy would be:

var scrollsave = $('body').scrollTop();
$('#thetable tbody').replaceWith(newtbody);
$('body').scrollTop(scrollsave);

but no joy. I haven't resorted to jQuery.ScrollTo yet.


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>


You could periodically check the div outerHeight, and set the scrollTop property once the div rendering has crossed the scroll value.

var g_Interval ;
var g_ScrollTop ;

function checkAndAdjustScroll() {
    var height=$('#content').outerHeight() ;
    if(height>g_ScrollTop) {
            $(window).scrollTop(g_ScrollTop) ;
            clearInterval(g_Interval) ;
    }
}

And whenever you update your div with AJAX content, do this

g_ScrollTop=$(window).scrollTop() ;
g_Interval=setInterval(checkAndAdjustScroll, 100) ;

You might need to make adjustments based on the offset of your div from the top of the page


Try to use .html() instead of .replaceWith():

$('#thetable tbody').html( newtbody.html() );.

I don't know about possible performance bottlenecks here, but it did the trick for me when I was trying to preserve the scroll position of a div, and it seems to do well with the scroll position of the entire page as well.


function gotoDiv(divId)
{
   $('#'+ divId).get(0).scrollIntoView();
}
.ScrollStyle
{
    max-height: 200px;
    overflow-y: scroll;
}
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<div id="main">

<h2 style="text-align:center;"> ScrollInto View Demo </h2>

<div id="first" style="background-color:skyblue">
<h3>This is First Div </h3>
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</div>
</br></br></br>
<div id="second" style="background-color:yellow">
<b>This is Second Division? </b></br></br></br>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).


Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</div>
</div>
</br></br>
<button class="btn btn-primary" onclick="gotoDiv('second')">Goto Second Div</button>
<button class="btn btn-primary" onclick="gotoDiv('first')">Goto First Div</button>
<button class="btn btn-primary" onclick="gotoDiv('main')">Gotop</button>

Use the particular element's Id to scroll that element into the view. Hope this help.

var element = document.getElementById("elementId");
element.scrollIntoView(); 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜