How to prevent scrolling on prepend?(如何防止在前面滚动?)
问题描述
我将内容添加到正文的顶部.有时,此内容可能有 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.
这篇关于如何防止在前面滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止在前面滚动?
基础教程推荐
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01