X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Foffset.js;h=c472f98520a65c578870dd8fb6101588730dc912;hb=20673d7e5836dda504b66730b528a8dae9787493;hp=59591caea68913c07c569b279ab314100bf895f6;hpb=08cf82e88e7a1f88da34ca251335a685889f8765;p=jquery.git diff --git a/src/offset.js b/src/offset.js index 59591ca..c472f98 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]; @@ -103,7 +105,7 @@ if ( "getBoundingClientRect" in document.documentElement ) { jQuery.offset = { initialize: function() { - var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0, + var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0, html = "
"; jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } ); @@ -142,23 +144,35 @@ jQuery.offset = { jQuery.offset.initialize(); if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) { - top += parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0; - left += parseFloat( jQuery.curCSS(body, "marginLeft", true) ) || 0; + top += parseFloat( jQuery.css(body, "marginTop") ) || 0; + left += parseFloat( jQuery.css(body, "marginLeft") ) || 0; } return { top: top, left: left }; }, setOffset: function( elem, options, i ) { + var position = jQuery.css( 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 ), + + 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 = {}; + curCSSTop = jQuery.css( elem, "top" ), + curCSSLeft = jQuery.css( elem, "left" ), + 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 ); @@ -198,12 +212,12 @@ jQuery.fn.extend({ // Subtract element margins // note: when an element has margin: auto the offsetLeft and marginLeft // are the same in Safari causing offset.left to incorrectly be 0 - offset.top -= parseFloat( jQuery.curCSS(elem, "marginTop", true) ) || 0; - offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0; + offset.top -= parseFloat( jQuery.css(elem, "marginTop") ) || 0; + offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0; // Add offsetParent borders - parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth", true) ) || 0; - parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0; + parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0; + parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0; // Subtract the two offsets return { @@ -269,3 +283,5 @@ function getWindow( elem ) { elem.defaultView || elem.parentWindow : false; } + +})( jQuery );