简体   繁体   English

在其上插入新的DOM元素时,保持文档不动

[英]Keep document not moving while new DOM element are inserted above it

I have a sequence of block elements on the page and new elements are being added at the top all the time, pushing the rest down. 我在页面上有一系列的块元素,并且始终在顶部添加新元素,将其余元素向下推。 I would like to make it so that once a user scrolls down and is above one block element, scrolling position follows that block element so that it does not move, even if new elements are added above te. 我要这样做,以便一旦用户向下滚动并在一个block元素上方,滚动位置就会跟随该block元素,因此即使在te上方添加了新元素,滚动位置也不会移动。

Yes. 是。 It's possible. 这是可能的。 If you have control of inserting the elements above, then you can calculate its height, and adjust the vertical scroll to accommodate. 如果您可以控制上述元素的插入,则可以计算其高度,并调整垂直滚动条以适应。 Keep in mind that scrolling is slightly different across browsers, so you would be safer using a framework like jQuery, which handles browser incompatibilities. 请记住,浏览器之间的滚动略有不同,因此使用类似jQuery的框架来处理浏览器不兼容问题会更安全。 Conversely, if you know your clients are using only one browser, then you can use straight javascript. 相反,如果您知道客户仅使用一种浏览器,则可以使用纯JavaScript。 The same issue applies to finding the height of an element added: your javascript code may work on some browsers and not others. 同样的问题也适用于查找添加的元素的高度:您的JavaScript代码可能在某些浏览器上有效,而在其他浏览器上则无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM