What would make offsetParent null?

前端 未结 7 862
清歌不尽
清歌不尽 2020-12-09 17:59

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 o

相关标签:
7条回答
  • 2020-12-09 18:25

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

    0 讨论(0)
  • 2020-12-09 18:26

    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)

    0 讨论(0)
  • 2020-12-09 18:32

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

    0 讨论(0)
  • 2020-12-09 18:38

    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.

    0 讨论(0)
  • 2020-12-09 18:41

    I also experienced an offsetParent of null when the parent was inside a web component (i.e. the element I tried getting the offsetParent of was projected into the web component).

    0 讨论(0)
  • 2020-12-09 18:43

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

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

    0 讨论(0)
提交回复
热议问题