Back out one of the changes from the previous commit that wasn't necessary to fix...
[jquery.git] / src / offset.js
index 3c63aae..895214a 100644 (file)
@@ -5,15 +5,16 @@ jQuery.fn.offset = function() {
        var left = 0, top = 0, elem = this[0], results;
        
        if ( elem ) with ( jQuery.browser ) {
-               var     absolute     = jQuery.css(elem, "position") == "absolute", 
-                   parent       = elem.parentNode, 
+               var     parent       = elem.parentNode, 
+                   offsetChild  = elem,
                    offsetParent = elem.offsetParent, 
                    doc          = elem.ownerDocument,
-                   safari2      = safari && parseInt(version) < 522;
+                   safari2      = safari && parseInt(version) < 522,
+                   fixed        = jQuery.css(elem, "position") == "fixed";
        
                // Use getBoundingClientRect if available
                if ( elem.getBoundingClientRect ) {
-                       box = elem.getBoundingClientRect();
+                       var box = elem.getBoundingClientRect();
                
                        // Add the document scroll offsets
                        add(
@@ -42,21 +43,23 @@ jQuery.fn.offset = function() {
                                add( offsetParent.offsetLeft, offsetParent.offsetTop );
                        
                                // Mozilla and Safari > 2 does not include the border on offset parents
-                               // However Mozilla adds the border for table cells
-                               if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 )
+                               // However Mozilla adds the border for table or table cells
+                               if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
                                        border( offsetParent );
-                               
-                               // Safari <= 2 doubles body offsets with an absolutely positioned element or parent
-                               if ( safari2 && !absolute && jQuery.css(offsetParent, "position") == "absolute" )
-                                       absolute = true;
+                                       
+                               // Add the document scroll offsets if position is fixed on any offsetParent
+                               if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
+                                       fixed = true;
                        
+                               // Set offsetChild to previous offsetParent unless it is the body element
+                               offsetChild  = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
                                // Get next offsetParent
                                offsetParent = offsetParent.offsetParent;
                        }
                
                        // Get parent scroll offsets
                        while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
-                               // Work around opera inline/table scrollLeft/Top bug
+                               // Remove parent scroll UNLESS that parent is inline or a table-row to work around Opera inline/table scrollLeft/Top bug
                                if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
                                        // Subtract parent scroll offsets
                                        add( -parent.scrollLeft, -parent.scrollTop );
@@ -69,9 +72,18 @@ jQuery.fn.offset = function() {
                                parent = parent.parentNode;
                        }
                
-                       // Safari doubles body offsets with an absolutely positioned element or parent
-                       if ( safari2 && absolute )
-                               add( -doc.body.offsetLeft, -doc.body.offsetTop );
+                       // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
+                       // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
+                       if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) || 
+                               (mozilla && jQuery.css(offsetChild, "position") != "absoltue") )
+                                       add( -doc.body.offsetLeft, -doc.body.offsetTop );
+                       
+                       // Add the document scroll offsets if position is fixed
+                       if ( fixed )
+                               add(
+                                       Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
+                                       Math.max(doc.documentElement.scrollTop,  doc.body.scrollTop)
+                               );
                }
 
                // Return an object with top and left properties