What would make offsetParent null?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 01:37:52

If the document hasn't finished loading then offsetParent can be null

I have made a test of 2,304 divs with unique combinations of values for position, display, and visibility, 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:

  • The element has position:fixed (Webkit and IE9)
  • The element has display:none (Webkit and FF)
  • Any ancestor has 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 have offsetParent==null.

https://developer.mozilla.org/en/DOM/element.offsetParent

offsetParent returns null when the element has style.display set to "none".

offsetParent will return null if your element object hasn't been appended to the DOM yet.

Mark

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)

I have run into this problem when the sibling just to the left of the element is hidden:

<div id="parent">
  <div id="element1">some stuff</div>
  <div id="element2" style="display: none">some hidden stuff</div>
  <div id="element3">child whose offset we want</div>
</div>

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!