From 36b4a9ba33b22b2394a1b190b0b4ade1be6d0b02 Mon Sep 17 00:00:00 2001 From: Brandon Aaron Date: Tue, 17 Mar 2009 20:43:46 +0000 Subject: [PATCH] some clean up in the offset method --- src/offset.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/offset.js b/src/offset.js index 69f624d..4370fe5 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 { top: 0, left: 0 }; + 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 { top: 0, left: 0 }; + 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), @@ -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 = '
'; - rules = { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' }; - for ( prop in rules ) container.style[prop] = rules[prop]; + jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } ); container.innerHTML = html; body.insertBefore(container, body.firstChild); -- 1.7.10.4