Home >Web Front-end >JS Tutorial >javascript YUI code reading diary YAHOO.util.Dom - Part.4_YUI.Ext related
var getXY = function() {
// Determine whether it is IE
if (document.documentElement.getBoundingClientRect) {
// Note 1
return function(el) {
var box = el.getBoundingClientRect();
var rootNode = el.ownerDocument; 🎜> Y.Dom.getDocumentScrollTop (rootNode)];
parentNode = el.offsetParent ; > // Determine whether the node is absolute under Safari,
// Whether the parent element is body
// Note 2.
var accountforBody = > Y.Dom.getStyle(el, 'position') == 'absolute' &&
el.offsetParent == el.ownerDocument.body);
// If the parent element Not itself
if (parentNode != el) {
; .offsettop;
y.DOM.GetStyle (Parentnode, 'Position')
== 'Absolute') {
AccountForbody = True;
}
Parentnod e = Parentnode.OffsetParent;
}
}
// 还是针对 Safari 的
if (accountForBody) { //safari doubles in this case
pos[0] -= el.ownerDocument.body.offsetLeft;
pos[1] -= el.ownerDocument.body.offsetTop;
}
parentNode = el.parentNode;
// account for any scrolled ancestors
while ( parentNode.tagName &&
!patterns.ROOT_TAG.test(parentNode.tagName) )
{
// work around opera inline/table scrollLeft/Top bug
// 注3.
if (Y.Dom.getStyle(parentNode, 'display')
.search(/^inline|table-row.*$/i)) {
pos[0] -= parentNode.scrollLeft;
pos[1] -= parentNode.scrollTop;
}
parentNode = parentNode.parentNode;
}
return pos;
};
}
}() // NOTE: Executing for loadtime branching注. 有关 IE 的 getBoundingClientRect 方法,可以参考这里。
注. Safari 的 BUG,详细情况参见这里。
注. 参见老外的原话(出处):
"- Remove parent scroll UNLESS that parent is inline or a table
to work around Opera inline/table scrollLeft/Top bug"
Fixed in Opera 9.5. (also, Opera 9.5 supports getBoundingClientRect
and getClientRects.)最后,有关更多 DOM 的兼容性,可以参看 PPK 的总结(怎么又是他)。