Created
August 3, 2011 09:28
-
-
Save erubboli/1122246 to your computer and use it in GitHub Desktop.
jquery.position.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.extend({ | |
position: function() { | |
if ( !this[0] ) { | |
return null; | |
} | |
var elem = this[0], | |
// Get *real* offsetParent | |
offsetParent = this.offsetParent(), | |
// Get correct offsets | |
offset = this.offset(), | |
parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset(); | |
// 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; | |
// Add offsetParent borders | |
parentOffset.top += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth", true) ) || 0; | |
parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0; | |
// Subtract the two offsets | |
return { | |
top: offset.top - parentOffset.top, | |
left: offset.left - parentOffset.left | |
}; | |
}, | |
offsetParent: function() { | |
return this.map(function() { | |
var offsetParent = this.offsetParent || document.body; | |
while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) { | |
offsetParent = offsetParent.offsetParent; | |
} | |
return offsetParent; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment