YAHOO. util. Dom-Part.4_YUI.Ext related to javascriptYUI code reading Diary

Source: Internet
Author: User
The getXY function in YAHOO. util. Dom allows developers to fully experience the Hack of different browsers. IE8 is about to break out of the shell. I hope the following functions won't be judged by multiple if statements. The getXY function uses an anonymous function to execute the return function (it sounds a bit tricky. refer to an article on the Blog of the center of the circle ). Var getXY = function (){
// Determine whether it is IE
If (document.doc umentElement. getBoundingClientRect ){
// Note 1
Return function (el ){
Var box = el. getBoundingClientRect ();

Var rootNode = el. ownerDocument;
Return [box. left +
Y. Dom. getDocumentScrollLeft (rootNode), box. top +
Y. Dom. getDocumentScrollTop (rootNode)];
};
} Else {
Return function (el ){
Var pos = [el. offsetLeft, el. offsetTop];
Var parentNode = el. offsetParent;

// Determine whether the node is absolute in Safari,
// And whether the parent element is body
// Note 2.
Var accountForBody = (isSafari &&
Y. Dom. getStyle (el, 'position') = 'absolute '&&
El. offsetParent = el. ownerDocument. body );

// If the parent element is not itself
If (parentNode! = El ){
While (parentNode ){
Pos [0] + = parentNode. offsetLeft;
Pos [1] + = parentNode. offsetTop;
If (! AccountForBody & isSafari &&
Y. Dom. getStyle (parentNode, 'position ')
= 'Absolute '){
AccountForBody = true;
}
ParentNode = parentNode. offsetParent;
}
}

// It is still for 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
// Note 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 NOTE. for the getBoundingClientRect method of IE, refer to here.
Note. For details about Safari BUG, see here.
Note. Refer to the old saying of foreigners (source ):
"-Remove parent scroll UNLESS that parent is inline or a table
To work around und Opera inline/table scrollLeft/Top bug"
Fixed in Opera 9.5. (also, Opera 9.5 supports getBoundingClientRect
And getClientRects.) Finally, for more information about DOM compatibility, see the summary of PPK (how is it true ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.