使用jQuery恢复页面滚动位置 [英] Restoring page scroll position with jQuery

查看:100
本文介绍了使用jQuery恢复页面滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用类似以下内容的页面操作:

I have a page operation that uses something like:

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

在ajax回调中.有时,如果用户已向下滚动页面,则此操作具有可以向上滚动页面的可理解的副作用.但是替换对用户而言似乎是无缝的,因此不得不再次向下滚动会很烦人.而且,由于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 normally 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);

从JS调试器控制台执行我希望的操作,我认为最简单的解决方法是:

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);

但没有喜悦.我还没有使用 jQuery.ScrollTo .

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);

这篇关于使用jQuery恢复页面滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆