尽管位置是绝对的,但是防止div滚动(prevent div from scrolling despite position: absolute)

编程入门 行业动态 更新时间:2024-10-23 07:32:58
尽管位置是绝对的,但是防止div滚动(prevent div from scrolling despite position: absolute)

我面临着一个'div hell'。 这是html片段:

<div id="xyz" class="sensoricon moveable type1 node-2-sensor-1 string1" style="position: absolute; top: 241.467px; left: 236.417px; opacity: 1;"> <img src="/content/images/shared/sensors/bla.png" alt=""> </div>

我明白那个:

position: absolute;

应该防止div滚动,但它仍然会(尽管开头只有一点点)。 任何人都可以看到一般错误吗?

I am facing a bit of a 'div hell'. This is the html snippet:

<div id="xyz" class="sensoricon moveable type1 node-2-sensor-1 string1" style="position: absolute; top: 241.467px; left: 236.417px; opacity: 1;"> <img src="/content/images/shared/sensors/bla.png" alt=""> </div>

I understand that:

position: absolute;

should prevent the div from scrolling, but it still does (although only a little bit at the beginning). Can anyone see anything generally wrong?

最满意答案

你想要做的是:

position: fixed;

因为位置:绝对; 设置相对于父坐标的位置,当滚动文档时,绝对位置相对于父级和父级(通过任何级别计数)仍相对于整个文档位置。

另一方面, position:fixed设置相对于windows而不是document的位置,这样滚动时它就会停留在同一个地方。

What you want to do is:

position: fixed;

Because position: absolute; sets position relative to parent coordinates and when the document is scrolled, absolute position is STILL relative to parent's and parents (through whatever count of levels) is relative to whole document position.

On the other hand position: fixed sets position relative to window's and not document's. That way your element stays in the same place when scrolled.

更多推荐

本文发布于:2023-07-16 07:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1125490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   div   prevent   absolute   position

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!