X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Foffset.js;h=fbefd7a27f921d5a9b4585697f5cf9a335edda15;hb=1d352084c4257217b0778f1ec3bb2e409411a4c3;hp=7ae9cb909f85d4e3b3157e0320cf061ae2654849;hpb=3776cbe314ac6f5081e998c3d5ac84d3fbc51872;p=jquery.git diff --git a/src/offset.js b/src/offset.js index 7ae9cb9..fbefd7a 100644 --- a/src/offset.js +++ b/src/offset.js @@ -2,24 +2,31 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; - if ( !elem || !elem.ownerDocument ) { - return null; - } - if ( options ) { - return this.each(function() { - jQuery.offset.setOffset( this, options ); + return this.each(function( i ) { + jQuery.offset.setOffset( this, options, i ); }); } + if ( !elem || !elem.ownerDocument ) { + return null; + } + if ( elem === elem.ownerDocument.body ) { return jQuery.offset.bodyOffset( elem ); } - var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement, - clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0, - top = box.top + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop, - left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft; + var box = elem.getBoundingClientRect(), + doc = elem.ownerDocument, + body = doc.body, + docElem = doc.documentElement, + win = getWindow(doc), + clientTop = docElem.clientTop || body.clientTop || 0, + clientLeft = docElem.clientLeft || body.clientLeft || 0, + scrollTop = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop || body.scrollTop ), + scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft), + top = box.top + scrollTop - clientTop, + left = box.left + scrollLeft - clientLeft; return { top: top, left: left }; }; @@ -28,16 +35,16 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; - if ( !elem || !elem.ownerDocument ) { - return null; - } - if ( options ) { - return this.each(function() { - jQuery.offset.setOffset( this, options ); + return this.each(function( i ) { + jQuery.offset.setOffset( this, options, i ); }); } + if ( !elem || !elem.ownerDocument ) { + return null; + } + if ( elem === elem.ownerDocument.body ) { return jQuery.offset.bodyOffset( elem ); } @@ -121,7 +128,7 @@ jQuery.offset = { body.removeChild( container ); body = container = innerDiv = checkDiv = table = td = null; - jQuery.offset.initialize = function() {}; + jQuery.offset.initialize = jQuery.noop; }, bodyOffset: function( body ) { @@ -137,7 +144,7 @@ jQuery.offset = { return { top: top, left: left }; }, - setOffset: function( elem, options ) { + setOffset: function( elem, options, i ) { // set position first, in-case top/left are set even on static elem if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) { elem.style.position = "relative"; @@ -145,11 +152,16 @@ jQuery.offset = { var curElem = jQuery( elem ), curOffset = curElem.offset(), curTop = parseInt( jQuery.curCSS( elem, "top", true ), 10 ) || 0, - curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10) || 0, - props = { - top: (options.top - curOffset.top) + curTop, - left: (options.left - curOffset.left) + curLeft - }; + curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0; + + if ( jQuery.isFunction( options ) ) { + options = options.call( elem, i, curOffset ); + } + + var props = { + top: (options.top - curOffset.top) + curTop, + left: (options.left - curOffset.left) + curLeft + }; if ( "using" in options ) { options.using.call( elem, props );