offset: fixes for correct body offsets in safari and mozilla (thanks Wizzud)

This commit is contained in:
Brandon Aaron 2007-09-26 22:28:00 +00:00
parent 92a9e73bbe
commit a9add215ea

View file

@ -6,12 +6,11 @@ jQuery.fn.offset = function() {
if ( elem ) with ( jQuery.browser ) { if ( elem ) with ( jQuery.browser ) {
var parent = elem.parentNode, var parent = elem.parentNode,
offsetChild = elem,
offsetParent = elem.offsetParent, offsetParent = elem.offsetParent,
doc = elem.ownerDocument, doc = elem.ownerDocument,
safari2 = safari && parseInt(version) < 522, safari2 = safari && parseInt(version) < 522,
position = jQuery.css(elem, "position"), fixed = jQuery.css(elem, "position") == "fixed";
absolute = position == "absolute",
fixed = position == "fixed";
// Use getBoundingClientRect if available // Use getBoundingClientRect if available
if ( elem.getBoundingClientRect ) { if ( elem.getBoundingClientRect ) {
@ -44,35 +43,23 @@ jQuery.fn.offset = function() {
add( offsetParent.offsetLeft, offsetParent.offsetTop ); add( offsetParent.offsetLeft, offsetParent.offsetTop );
// Mozilla and Safari > 2 does not include the border on offset parents // Mozilla and Safari > 2 does not include the border on offset parents
// However Mozilla adds the border for table cells // However Mozilla adds the border for table or table cells
if ( mozilla && /^t[d|h]$/i.test(parent.tagName) || !safari2 ) if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
border( offsetParent ); border( offsetParent );
// Get offsetParent's position // Add the document scroll offsets if position is fixed on any offsetParent
position = jQuery.css(offsetParent, "position"); if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
// Safari <= 2 doubles body offsets with an absolutely positioned element or parent
if ( safari2 && !absolute && position == "absolute" )
absolute = true;
// Opera adds border for fixed, relative and absolute parent elements
if (opera && /^fixed|relative|absolute$/i.test(position))
add(
-parseInt(jQuery.css(elem, "borderLeftWidth")),
-parseInt(jQuery.css(elem, "borderTopWidth"))
);
// Add the document scroll offsets if position is fixed
if ( !fixed && position == "fixed" )
fixed = true; fixed = true;
// Set offsetChild to previous offsetParent unless it is the body element
offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
// Get next offsetParent // Get next offsetParent
offsetParent = offsetParent.offsetParent; offsetParent = offsetParent.offsetParent;
} }
// Get parent scroll offsets // Get parent scroll offsets
while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) { while ( parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
// Work around opera inline/table scrollLeft/Top bug // Remove parent scroll UNLESS that parent is inline or a table-row to work around Opera inline/table scrollLeft/Top bug
if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) ) if ( !/^inline|table-row.*$/i.test(jQuery.css(parent, "display")) )
// Subtract parent scroll offsets // Subtract parent scroll offsets
add( -parent.scrollLeft, -parent.scrollTop ); add( -parent.scrollLeft, -parent.scrollTop );
@ -85,9 +72,11 @@ jQuery.fn.offset = function() {
parent = parent.parentNode; parent = parent.parentNode;
} }
// Safari <= 2 doubles body offsets with an absolute or fixed positioned element or parent // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
if ( safari2 && (absolute || fixed) ) // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
add( -doc.body.offsetLeft, -doc.body.offsetTop ); if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) ||
(mozilla && jQuery.css(offsetChild, "position") != "absoltue") )
add( -doc.body.offsetLeft, -doc.body.offsetTop );
// Add the document scroll offsets if position is fixed // Add the document scroll offsets if position is fixed
if ( fixed ) if ( fixed )
@ -109,6 +98,6 @@ jQuery.fn.offset = function() {
function add(l, t) { function add(l, t) {
left += parseInt(l) || 0; left += parseInt(l) || 0;
top += parseInt(t) || 0; top += parseInt(t) || 0;
} }
}; };