我将内容添加到正文的顶部。有时该内容的高度可能为 400-500 像素,当添加类似的内容时,在阅读页面时将内容向下推可能会非常烦人。
我希望这些项目自动添加到前面,而不是像单击此处查看新项目那样。
有没有办法在不移动页面的情况下将此内容添加到正文顶部?那么当用户滚动到顶部时它就已经在那里了吗?
我相信最通用的方法是在修改文档之前和之后简单地测量文档高度:
var old_height = $(document).height(); //store document height before modifications
var old_scroll = $(window).scrollTop(); //remember the scroll position
//do anything
$("p:first").prepend( "<p>I just became first</p>" );
$(document).scrollTop(old_scroll + $(document).height() - old_height); //restore "scroll position"
这样您就可以真正执行任何操作,而窗口不会离开您正在阅读的内容:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)