SoFunction
Updated on 2025-04-13

javascript YUI code reading diary - Part.4

var getXY = function() {
// Judge whether it is IE
    if () {
// Note 1
        return function(el) {
            var box = ();

            var rootNode = ;
            return [ + 
          (rootNode),  +
                    (rootNode)];
        };
    } else {
        return function(el) {
            var pos = [, ];
            var parentNode = ;

// Determine whether the node is absolute under Safari,
// And whether the parent element is a body
Note 2.
            var accountForBody = (isSafari &&
                    (el, 'position') == 'absolute' &&
                     == );

// If the parent element is not itself
            if (parentNode != el) {
                while (parentNode) {
                    pos[0] += ;
                    pos[1] += ;
                    if (!accountForBody && isSafari && 
                            (parentNode,'position') 
                                                  == 'absolute' ) { 
                        accountForBody = true;
                    }
                    parentNode = ;
                }
            }

// It's still for Safari
            if (accountForBody) { //safari doubles in this case
                pos[0] -= ;
                pos[1] -= ;
            } 
            parentNode = ;

            // account for any scrolled ancestors
            while (  && 
                         !patterns.ROOT_TAG.test() ) 
            {
               // work around opera inline/table scrollLeft/Top bug
Note 3.
               if ((parentNode, 'display')
                                  .search(/^inline|table-row.*$/i)) { 
                    pos[0] -= ;
                    pos[1] -= ;
                }

                parentNode = ; 
            }

            return pos;
        };
    }
}()// Note: Executing for loadtime branching Note. For IE ’s getBoundingClientRect method, you can refer to this.
Note. Safari’s BUG, ​​please see here for details.
Note. See foreigners’ original words (source):
"- 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.) Finally, for more DOM compatibility, you can refer to PPK’s summary (why it is him again).