X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Foffset.js;h=c38a7f2163e1c55733731478f9c61026f46bcff5;hb=bca576550249e9b79b1097669dff6d4ddd0d65cf;hp=365b35aca8ec5091babf1c15a82351c1a5f81bbe;hpb=fc08d0ea8dc881ed56102eb14e3ad9c54cccc34e;p=jquery.git diff --git a/src/offset.js b/src/offset.js index 365b35a..c38a7f2 100644 --- a/src/offset.js +++ b/src/offset.js @@ -1,3 +1,5 @@ +(function( jQuery ) { + if ( "getBoundingClientRect" in document.documentElement ) { jQuery.fn.offset = function( options ) { var elem = this[0]; @@ -150,23 +152,38 @@ jQuery.offset = { }, setOffset: function( elem, options, i ) { + var position = jQuery.curCSS( elem, "position" ); + // set position first, in-case top/left are set even on static elem - if ( /static/.test( jQuery.curCSS( elem, "position" ) ) ) { + if ( position === "static" ) { elem.style.position = "relative"; } - 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; + + var curElem = jQuery( elem ), + curOffset = curElem.offset(), + curCSSTop = jQuery.curCSS( elem, "top", true ), + curCSSLeft = jQuery.curCSS( elem, "left", true ), + calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1), + props = {}, curPosition = {}, curTop, curLeft; + + // need to be able to calculate position if either top or left is auto and position is absolute + if ( calculatePosition ) { + curPosition = curElem.position(); + } + + curTop = calculatePosition ? curPosition.top : parseInt( curCSSTop, 10 ) || 0; + curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 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 (options.top != null) { + props.top = (options.top - curOffset.top) + curTop; + } + if (options.left != null) { + props.left = (options.left - curOffset.left) + curLeft; + } if ( "using" in options ) { options.using.call( elem, props ); @@ -266,3 +283,5 @@ function getWindow( elem ) { elem.defaultView || elem.parentWindow : false; } + +})( jQuery );