diff options
author | Merrifield, Jay <jmerrifiel@gannett.com> | 2012-10-17 13:40:52 -0400 |
---|---|---|
committer | Mike Sherov <mike.sherov@gmail.com> | 2012-10-17 13:41:49 -0400 |
commit | 425272aea1fb30b734bac25b8f74dbe85a3ce215 (patch) | |
tree | 9d091e857914123efebbea72e5ab64c8c0cedd8b /src/offset.js | |
parent | 995f816cf4e0a404075c27e0d249db0b4a07a97e (diff) | |
download | jquery-425272aea1fb30b734bac25b8f74dbe85a3ce215.tar.gz jquery-425272aea1fb30b734bac25b8f74dbe85a3ce215.zip |
Fixes #12749, correctly detect position() for position:fixed elements, closes gh-991
Diffstat (limited to 'src/offset.js')
-rw-r--r-- | src/offset.js | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/offset.js b/src/offset.js index 381a42da2..6a2fdc993 100644 --- a/src/offset.js +++ b/src/offset.js @@ -107,33 +107,39 @@ jQuery.offset = { jQuery.fn.extend({ position: function() { - if ( !this[0] ) { + if ( !this[ 0 ] ) { return; } - var elem = this[0], + var offsetParent, offset, + parentOffset = { top: 0, left: 0 }, + elem = this[ 0 ]; - // Get *real* offsetParent - offsetParent = this.offsetParent(), + // fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is it's only offset parent + if ( jQuery.css( elem, "position" ) === "fixed" ) { + // we assume that getBoundingClientRect is available when computed position is fixed + offset = elem.getBoundingClientRect(); + } else { + // Get *real* offsetParent + offsetParent = this.offsetParent(); + + // Get correct offsets + offset = this.offset(); + if ( !rroot.test( offsetParent[ 0 ].nodeName ) ) { + parentOffset = offsetParent.offset(); + } - // Get correct offsets - offset = this.offset(), - parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset(); + // Add offsetParent borders + parentOffset.top += parseFloat( jQuery.css( offsetParent[ 0 ], "borderTopWidth" ) ) || 0; + parentOffset.left += parseFloat( jQuery.css( offsetParent[ 0 ], "borderLeftWidth" ) ) || 0; + } - // Subtract element margins + // Subtract parent offsets and 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.css(elem, "marginTop") ) || 0; - offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0; - - // Add offsetParent borders - parentOffset.top += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0; - parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0; - - // Subtract the two offsets return { - top: offset.top - parentOffset.top, - left: offset.left - parentOffset.left + top: offset.top - parentOffset.top - ( parseFloat( jQuery.css( elem, "marginTop" ) ) || 0 ), + left: offset.left - parentOffset.left - ( parseFloat( jQuery.css( elem, "marginLeft" ) ) || 0 ) }; }, |