X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Foffset.js;h=bca01d74d4441032e566449b5052fed9b7c3111f;hb=2c31a1624f72df3987cd1d0d3605c359658986e5;hp=69f624dbe46c8dd56ffec49f52e4bd254b145622;hpb=7647cebc3929337d1e77069c8620158dcf84ff20;p=jquery.git diff --git a/src/offset.js b/src/offset.js index 69f624d..bca01d7 100644 --- a/src/offset.js +++ b/src/offset.js @@ -1,8 +1,9 @@ -if ( document.documentElement["getBoundingClientRect"] ) +if ( "getBoundingClientRect" in document.documentElement ) jQuery.fn.offset = function() { - if ( !this[0] ) return { top: 0, left: 0 }; - if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] ); - var box = this[0].getBoundingClientRect(), doc = this[0].ownerDocument, body = doc.body, docElem = doc.documentElement, + var elem = this[0]; + if ( !elem ) 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.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop, left = box.left + (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft; @@ -10,11 +11,12 @@ if ( document.documentElement["getBoundingClientRect"] ) }; else jQuery.fn.offset = function() { - if ( !this[0] ) return { top: 0, left: 0 }; - if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] ); + var elem = this[0]; + if ( !elem ) return null; + if ( elem === elem.ownerDocument.body ) return jQuery.offset.bodyOffset( elem ); jQuery.offset.initialized || jQuery.offset.initialize(); - var elem = this[0], offsetParent = elem.offsetParent, prevOffsetParent = elem, + var offsetParent = elem.offsetParent, prevOffsetParent = elem, doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement, body = doc.body, defaultView = doc.defaultView, prevComputedStyle = defaultView.getComputedStyle(elem, null), @@ -26,13 +28,13 @@ else if ( elem === offsetParent ) { top += elem.offsetTop, left += elem.offsetLeft; if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) ) - top += parseInt( computedStyle.borderTopWidth, 10) || 0, - left += parseInt( computedStyle.borderLeftWidth, 10) || 0; + top += parseFloat( computedStyle.borderTopWidth, 10) || 0, + left += parseFloat( computedStyle.borderLeftWidth, 10) || 0; prevOffsetParent = offsetParent, offsetParent = elem.offsetParent; } if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) - top += parseInt( computedStyle.borderTopWidth, 10) || 0, - left += parseInt( computedStyle.borderLeftWidth, 10) || 0; + top += parseFloat( computedStyle.borderTopWidth, 10) || 0, + left += parseFloat( computedStyle.borderLeftWidth, 10) || 0; prevComputedStyle = computedStyle; } @@ -50,11 +52,10 @@ else jQuery.offset = { initialize: function() { if ( this.initialized ) return; - var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, rules, prop, bodyMarginTop = body.style.marginTop, + var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, prop, bodyMarginTop = body.style.marginTop, html = '