什么会使 offsetParent 为空?
我正在尝试用 JavaScript 进行定位。 我正在使用基于 经典 quirksmode 函数 的累积位置函数计算每个 offsetParent
的 offsetTop
和 offsetLeft
的总和,直到顶部节点。
但是,我遇到了一个问题,我感兴趣的元素在 Firefox 中没有 offsetParent
。 IE 中存在 offsetParent
,但 offsetTop
和 offsetLeft
的总和为 0,因此与 Firefox 中存在同样的问题。
什么会导致屏幕上清晰可见且可用的元素没有 offsetParent
? 或者,更实际的是,我如何找到该元素的位置以便在其下方放置一个下拉菜单?
编辑:以下是如何重现此问题的一个特定实例(当前接受的答案未解决):
- 打开 Stack Overflow 的 主页。
在网络浏览器(例如Chromev21)的控制台中运行以下代码:
var e = document.querySelector('div'); 控制台.log(e); // ; 做{ var s = getComputedStyle(e); console.log(e.tagName,s.display,s.visibility,s.position,e.offsetParent); while(e=e.parentElement) // DIV块可见固定null // BODY 块可见 static null // HTML 块可见 static null
为什么该元素的 offsetParent
为 null
?
I am trying to do positioning in JavaScript. I am using a cumulative position function based on the classic quirksmode function that sums offsetTop
and offsetLeft
for each offsetParent
until the top node.
However, I am running into an issue where the element I'm interested in has no offsetParent
in Firefox. In IE offsetParent
exists, but offsetTop
and offsetLeft
all sum up to 0, so it has the same problem in effect as in Firefox.
What would cause an element that is clearly visible and usable on the screen to not have an offsetParent
? Or, more practically, how can I find the position of this element in order to place a drop-down beneath it?
Edit: Here's how to reproduce one particular instance of this (not solved by the currently-accepted answer):
- Open the home page of Stack Overflow.
Run the following code in the Console of the web browser (e.g. Chromev21):
var e = document.querySelector('div'); console.log(e); // <div id="notify-container"></div> do{ var s = getComputedStyle(e); console.log(e.tagName,s.display,s.visibility,s.position,e.offsetParent); } while(e=e.parentElement) // DIV block visible fixed null // BODY block visible static null // HTML block visible static null
Why is the offsetParent
of that element null
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我已经对 2,304 个 div 进行了测试,并使用
position
、display 的值的独特组合
和可见性
,嵌套在每个值的唯一组合内,并确定:一个否则有效的元素
这是
的后代
如果满足以下条件,则不会有
offsetParent
值:position:fixed
(Webkit 和 IE9)display:none< /code>(Webkit 和 FF)
display:none
(Webkit 和 FF)也可以合理地预期没有父元素或未添加到页面本身的元素(不是页面
的后代)也将具有
offsetParent==null< /代码>.
I have made a test of 2,304 divs with unique combinations of values for
position
,display
, andvisibility
, nested inside unique combinations of each of those values, and determined that:an otherwise-valid element
that is a descendant of
<body>
will not have an
offsetParent
value if:position:fixed
(Webkit and IE9)display:none
(Webkit and FF)display:none
(Webkit and FF)It is also reasonable to expect that an element that has no parent, or that is not added to the page itself (is not a descendant of the
<body>
of the page), will also haveoffsetParent==null
.如果文档尚未完成加载,则 offsetParent 可以为 null
If the document hasn't finished loading then offsetParent can be null
https://developer.mozilla.org/en/DOM/element.offsetParent
当元素的 style.display 设置为“none”时,offsetParent 返回 null。
https://developer.mozilla.org/en/DOM/element.offsetParent
offsetParent returns null when the element has style.display set to "none".
如果您的元素对象尚未附加到 DOM,则
offsetParent
将返回 null。offsetParent
will return null if your element object hasn't been appended to the DOM yet.这是一个老问题,但我还有另一个案例。 如果您操作 DOM,则最终可能会得到 null offsetParent - 在 IE6 和 IE7 中。
请参阅: IE 6/7 - 访问 offsetParent (Javascript) 时出现“未指定错误”
This is an old question, but I have another case. If you manipulate the DOM, you may end up with a null offsetParent - in IE6 and IE7.
See: IE 6/7 - "Unspecified Error" when accessing offsetParent (Javascript)
当元素左侧的同级元素被隐藏时,我遇到了这个问题:
我遇到了 element3 的 offsetParent 为 null 的情况,即使 < strong>element3 本身可见,parent 可见。
我见过 Firefox 3.6 和 Chrome 5。它似乎也会影响 element3 上的 getBoundingClientRect() 函数,这真的很烦人,因为它适用于这么多其他情况!
I have run into this problem when the sibling just to the left of the element is hidden:
I've run into the case where the offsetParent of element3 is null even though element3 itself is visible, and parent is visible.
I've seen thin is Firefox 3.6 and Chrome 5. It seems to also affect the getBoundingClientRect() function on the element3, which is really annoying since that works in so many other cases!
当父级位于 Web 组件内时,我还遇到了
offsetParent
为null
的情况(即我尝试获取offsetParent
的元素被投影到网络组件)。I also experienced an
offsetParent
ofnull
when the parent was inside a web component (i.e. the element I tried getting theoffsetParent
of was projected into the web component).