問題描述
我將內容添加到正文的頂部.有時,此內容可能有 400-500 像素高,當添加此類內容時,在閱讀頁面時將內容向下推會非常煩人.
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.
這篇關于如何防止在前面滾動?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!