What would make offsetParent null?

前端 未结 7 908
清歌不尽
清歌不尽 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: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
    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 of the page), will also have offsetParent==null.

提交回复
热议问题