在看jquery的offsetParent源码实现时,如下
offsetParent: function() {
return this.map(function() {
var offsetParent = this.offsetParent || docElem;
while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" )
&& jQuery.css( offsetParent, "position") === "static" ) ) {
offsetParent = offsetParent.offsetParent;
}
return offsetParent || docElem;
});
}
对这一个offsetParent的while循环疑惑,关于属性offsetParent,MDN有描述 传送门
The HTMLElement.offsetParent read-only property returns a reference to the object which is the closest (nearest in the containment hierarchy) positioned containing element. If the element is non-positioned, the nearest table cell or root element (html in standards compliant mode; body in quirks rendering mode) is the offsetParent. offsetParent returns null when the element has style.display set to "none". The offsetParent is useful because offsetTop and offsetLeft are relative to its padding edge.
即offsetParent返回的是祖先元素里最接近的定位元素,那么既然是定位元素,其position,也就不应该是static了吧,当然body除外(即当祖先元素都没有定位的情况下)
那么源码中jQuery.css( offsetParent, "position") === "static" )
这一个判断到底是为何?
只是为了判断body的情况?
还有其他情况么?