2007-09-08 20:02:39 +02:00
|
|
|
// The Offset Method
|
|
|
|
// Originally By Brandon Aaron, part of the Dimension Plugin
|
|
|
|
// http://jquery.com/plugins/project/dimensions
|
2007-09-08 16:44:29 +02:00
|
|
|
jQuery.fn.offset = function() {
|
|
|
|
var left = 0, top = 0, elem = this[0], results;
|
|
|
|
|
|
|
|
if ( elem ) with ( jQuery.browser ) {
|
2007-12-12 17:55:55 +01:00
|
|
|
var parent = elem.parentNode,
|
2007-09-27 00:28:00 +02:00
|
|
|
offsetChild = elem,
|
2007-09-11 04:13:00 +02:00
|
|
|
offsetParent = elem.offsetParent,
|
|
|
|
doc = elem.ownerDocument,
|
2007-09-21 23:41:56 +02:00
|
|
|
safari2 = safari && parseInt(version) < 522,
|
2007-09-27 00:28:00 +02:00
|
|
|
fixed = jQuery.css(elem, "position") == "fixed";
|
2007-09-08 16:44:29 +02:00
|
|
|
|
|
|
|
// Use getBoundingClientRect if available
|
|
|
|
if ( elem.getBoundingClientRect ) {
|
2007-09-17 20:20:51 +02:00
|
|
|
var box = elem.getBoundingClientRect();
|
2007-09-08 16:44:29 +02:00
|
|
|
|
|
|
|
// Add the document scroll offsets
|
2007-12-12 17:55:55 +01:00
|
|
|
add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
|
|
|
|
box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
|
2007-09-08 16:44:29 +02:00
|
|
|
|
|
|
|
// IE adds the HTML element's border, by default it is medium which is 2px
|
2007-12-04 21:54:23 +01:00
|
|
|
// IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
|
2007-09-08 16:44:29 +02:00
|
|
|
// IE 7 standards mode, the border is always 2px
|
2007-12-04 21:54:23 +01:00
|
|
|
// This border/offset is typically represented by the clientLeft and clientTop properties
|
|
|
|
// However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS
|
|
|
|
// Therefore this method will be off by 2px in IE while in quirksmode
|
|
|
|
add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop );
|
2007-09-08 16:44:29 +02:00
|
|
|
|
|
|
|
// Otherwise loop through the offsetParents and parentNodes
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Initial element offsets
|
|
|
|
add( elem.offsetLeft, elem.offsetTop );
|
2007-12-12 17:55:55 +01:00
|
|
|
|
2007-09-08 16:44:29 +02:00
|
|
|
// Get parent offsets
|
|
|
|
while ( offsetParent ) {
|
|
|
|
// Add offsetParent offsets
|
|
|
|
add( offsetParent.offsetLeft, offsetParent.offsetTop );
|
|
|
|
|
|
|
|
// Mozilla and Safari > 2 does not include the border on offset parents
|
2007-09-27 00:28:00 +02:00
|
|
|
// However Mozilla adds the border for table or table cells
|
|
|
|
if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
|
2007-09-08 16:44:29 +02:00
|
|
|
border( offsetParent );
|
2007-09-21 23:41:56 +02:00
|
|
|
|
2007-09-27 00:28:00 +02:00
|
|
|
// Add the document scroll offsets if position is fixed on any offsetParent
|
|
|
|
if ( !fixed && jQuery.css(offsetParent, "position") == "fixed" )
|
2007-09-21 23:41:56 +02:00
|
|
|
fixed = true;
|
2007-09-08 16:44:29 +02:00
|
|
|
|
2007-09-27 00:28:00 +02:00
|
|
|
// Set offsetChild to previous offsetParent unless it is the body element
|
|
|
|
offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
|
2007-09-08 16:44:29 +02:00
|
|
|
// Get next offsetParent
|
|
|
|
offsetParent = offsetParent.offsetParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get parent scroll offsets
|
2007-12-19 20:07:12 +01:00
|
|
|
while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
|
2007-12-12 17:55:55 +01:00
|
|
|
// Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug
|
|
|
|
if ( !/^inline|table.*$/i.test(jQuery.css(parent, "display")) )
|
2007-09-08 16:44:29 +02:00
|
|
|
// Subtract parent scroll offsets
|
|
|
|
add( -parent.scrollLeft, -parent.scrollTop );
|
|
|
|
|
|
|
|
// Mozilla does not add the border for a parent that has overflow != visible
|
|
|
|
if ( mozilla && jQuery.css(parent, "overflow") != "visible" )
|
|
|
|
border( parent );
|
|
|
|
|
|
|
|
// Get next parent
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
|
2007-09-27 00:28:00 +02:00
|
|
|
// Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
|
|
|
|
// Mozilla doubles body offsets with a non-absolutely positioned offsetChild
|
|
|
|
if ( (safari2 && (fixed || jQuery.css(offsetChild, "position") == "absolute")) ||
|
2007-12-17 15:37:26 +01:00
|
|
|
(mozilla && jQuery.css(offsetChild, "position") != "absolute") )
|
2007-09-27 00:28:00 +02:00
|
|
|
add( -doc.body.offsetLeft, -doc.body.offsetTop );
|
2007-09-21 23:41:56 +02:00
|
|
|
|
|
|
|
// Add the document scroll offsets if position is fixed
|
|
|
|
if ( fixed )
|
2007-12-12 17:55:55 +01:00
|
|
|
add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
|
|
|
|
Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
|
2007-09-08 16:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an object with top and left properties
|
|
|
|
results = { top: top, left: left };
|
|
|
|
}
|
|
|
|
|
|
|
|
function border(elem) {
|
2007-12-19 20:05:34 +01:00
|
|
|
add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) );
|
2007-09-08 16:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function add(l, t) {
|
|
|
|
left += parseInt(l) || 0;
|
2007-09-27 00:28:00 +02:00
|
|
|
top += parseInt(t) || 0;
|
2007-09-08 16:44:29 +02:00
|
|
|
}
|
2007-12-16 02:03:50 +01:00
|
|
|
|
|
|
|
return results;
|
2007-09-08 16:44:29 +02:00
|
|
|
};
|