如何防止在前置滚动? [英] How to prevent scrolling on prepend?
问题描述
我将内容放在身体顶部.有时,此内容的高度可能为400-500px,并且当添加类似内容的内容时,在阅读页面时将其下推可能会很烦人.
I'm prepending content to the top of the body. Sometimes this content can be 400-500px tall, and when something like this gets added, pushing the content down when you are reading the page it can be really annoying.
我希望自动添加商品,而不是单击此处查看新商品.
I want the items to be automatically prepended, not something like click here to see new items.
是否有任何方法可以在不移动页面的情况下将此内容放在正文的顶部?然后,当用户滚动到顶部时,它已经在那里了吗?
Is there any way to prepend this content to the top of the body without moving the page? Then when a user scrolls to the top it is just there already?
推荐答案
我过去通过添加元素,然后计算元素的总外部高度并将滚动条设置为该值来完成此操作.像这样:
I've done it in the past by prepending the elements, then calculating their total outerheight, and setting the scrolltop to that value. Something like this:
var $current_top_element = $('body').children().first();
$('body').prepend(other_elements);
var previous_height = 0;
$current_top_element.prevAll().each(function() {
previous_height += $(this).outerHeight();
});
$('body').scrollTop(previous_height);
希望这会为您指明正确的方向.
Hope this points you in the right direction.
这篇关于如何防止在前置滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!