Added some unit tests for position method. Fixed issue with position in IE.
[jquery.git] / src / offset.js
index 4de37a0..b77a0cd 100644 (file)
@@ -91,8 +91,8 @@ jQuery.fn.offset = function() {
        }
 
        function add(l, t) {
-               left += parseInt(l) || 0;
-               top += parseInt(t) || 0;
+               left += parseInt(l, 10) || 0;
+               top += parseInt(t, 10) || 0;
        }
 
        return results;
@@ -101,23 +101,23 @@ jQuery.fn.offset = function() {
 
 jQuery.fn.extend({
        position: function() {
-               var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
+               var left = 0, top = 0, results;
 
-               if (elem) {
+               if ( this[0] ) {
                        // Get *real* offsetParent
-                       offsetParent = this.offsetParent();
+                       var offsetParent = this.offsetParent(),
 
                        // Get correct offsets
-                       offset       = this.offset();
-                       parentOffset = offsetParent.offset();
+                       offset       = this.offset(),
+                       parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
 
                        // Subtract element margins
-                       offset.top  -= parseInt( jQuery.curCSS(elem, 'marginTop', true) ) || 0;
-                       offset.left -= parseInt( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
+                       offset.top  -= parseInt( jQuery.curCSS( this[0], 'marginTop',  true ), 10 ) || 0;
+                       offset.left -= parseInt( jQuery.curCSS( this[0], 'marginLeft', true ), 10 ) || 0;
 
                        // Add offsetParent borders
-                       parentOffset.top  += parseInt( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true) ) || 0;
-                       parentOffset.left += parseInt( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
+                       parentOffset.top  += parseInt( jQuery.curCSS( offsetParent[0], 'borderTopWidth',  true ), 10 ) || 0;
+                       parentOffset.left += parseInt( jQuery.curCSS( offsetParent[0], 'borderLeftWidth', true ), 10 ) || 0;
 
                        // Subtract the two offsets
                        results = {
@@ -140,7 +140,9 @@ jQuery.fn.extend({
 
 // Create scrollLeft and scrollTop methods
 jQuery.each( ['Left', 'Top'], function(i, name) {
-       jQuery.fn[ 'scroll' + name ] = function(val) {
+       var method = 'scroll' + name;
+       
+       jQuery.fn[ method ] = function(val) {
                if (!this[0]) return;
 
                return val != undefined ?
@@ -149,17 +151,17 @@ jQuery.each( ['Left', 'Top'], function(i, name) {
                        this.each(function() {
                                this == window || this == document ?
                                        window.scrollTo(
-                                               name == 'Left' ? val : jQuery(window)[ 'scrollLeft' ](),
-                                               name == 'Top'  ? val : jQuery(window)[ 'scrollTop'  ]()
+                                               !i ? val : jQuery(window).scrollLeft(),
+                                                i ? val : jQuery(window).scrollTop()
                                        ) :
-                                       this[ 'scroll' + name ] = val;
+                                       this[ method ] = val;
                        }) :
 
                        // Return the scroll offset
                        this[0] == window || this[0] == document ?
-                               self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
-                                       jQuery.boxModel && document.documentElement[ 'scroll' + name ] ||
-                                       document.body[ 'scroll' + name ] :
-                               this[0][ 'scroll' + name ];
+                               self[ i ? 'pageYOffset' : 'pageXOffset' ] ||
+                                       jQuery.boxModel && document.documentElement[ method ] ||
+                                       document.body[ method ] :
+                               this[0][ method ];
        };
 });