2007-09-08 18:02:39 +00:00
|
|
|
// The Offset Method
|
|
|
|
// Originally By Brandon Aaron, part of the Dimension Plugin
|
|
|
|
// http://jquery.com/plugins/project/dimensions
|
2007-09-08 14:44:29 +00:00
|
|
|
jQuery.fn.offset = function() {
|
|
|
|
var left = 0, top = 0, elem = this[0], results;
|
|
|
|
|
|
|
|
if ( elem ) with ( jQuery.browser ) {
|
2007-12-12 16:55:55 +00:00
|
|
|
var parent = elem.parentNode,
|
2007-09-26 22:28:00 +00:00
|
|
|
offsetChild = elem,
|
2007-09-11 02:13:00 +00:00
|
|
|
offsetParent = elem.offsetParent,
|
|
|
|
doc = elem.ownerDocument,
|
2008-02-06 03:48:22 +00:00
|
|
|
safari2 = safari && parseInt(version) < 522 && !/adobeair/i.test(userAgent),
|
2008-04-21 22:54:46 +00:00
|
|
|
css = jQuery.curCSS,
|
|
|
|
fixed = css(elem, "position") == "fixed";
|
2007-09-08 14:44:29 +00:00
|
|
|
|
|
|
|
// Use getBoundingClientRect if available
|
|
|
|
if ( elem.getBoundingClientRect ) {
|
2007-09-17 18:20:51 +00:00
|
|
|
var box = elem.getBoundingClientRect();
|
2007-09-08 14:44:29 +00:00
|
|
|
|
|
|
|
// Add the document scroll offsets
|
2007-12-12 16:55:55 +00: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 14:44:29 +00:00
|
|
|
|
|
|
|
// IE adds the HTML element's border, by default it is medium which is 2px
|
2007-12-04 20:54:23 +00:00
|
|
|
// IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
|
2007-09-08 14:44:29 +00:00
|
|
|
// IE 7 standards mode, the border is always 2px
|
2007-12-04 20:54:23 +00: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 14:44:29 +00:00
|
|
|
|
|
|
|
// Otherwise loop through the offsetParents and parentNodes
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Initial element offsets
|
|
|
|
add( elem.offsetLeft, elem.offsetTop );
|
2007-12-12 16:55:55 +00:00
|
|
|
|
2007-09-08 14:44:29 +00: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-26 22:28:00 +00: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 14:44:29 +00:00
|
|
|
border( offsetParent );
|
2007-09-21 21:41:56 +00:00
|
|
|
|
2007-09-26 22:28:00 +00:00
|
|
|
// Add the document scroll offsets if position is fixed on any offsetParent
|
2008-04-21 22:54:46 +00:00
|
|
|
if ( !fixed && css(offsetParent, "position") == "fixed" )
|
2007-09-21 21:41:56 +00:00
|
|
|
fixed = true;
|
2007-09-08 14:44:29 +00:00
|
|
|
|
2007-09-26 22:28:00 +00:00
|
|
|
// Set offsetChild to previous offsetParent unless it is the body element
|
|
|
|
offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
|
2007-09-08 14:44:29 +00:00
|
|
|
// Get next offsetParent
|
|
|
|
offsetParent = offsetParent.offsetParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get parent scroll offsets
|
2007-12-19 19:07:12 +00:00
|
|
|
while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
|
2007-12-12 16:55:55 +00:00
|
|
|
// Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug
|
2008-04-21 22:54:46 +00:00
|
|
|
if ( !/^inline|table.*$/i.test(css(parent, "display")) )
|
2007-09-08 14:44:29 +00:00
|
|
|
// Subtract parent scroll offsets
|
|
|
|
add( -parent.scrollLeft, -parent.scrollTop );
|
|
|
|
|
|
|
|
// Mozilla does not add the border for a parent that has overflow != visible
|
2008-04-21 22:54:46 +00:00
|
|
|
if ( mozilla && css(parent, "overflow") != "visible" )
|
2007-09-08 14:44:29 +00:00
|
|
|
border( parent );
|
|
|
|
|
|
|
|
// Get next parent
|
|
|
|
parent = parent.parentNode;
|
|
|
|
}
|
|
|
|
|
2007-09-26 22:28:00 +00: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
|
2008-04-21 22:54:46 +00:00
|
|
|
if ( (safari2 && (fixed || css(offsetChild, "position") == "absolute")) ||
|
|
|
|
(mozilla && css(offsetChild, "position") != "absolute") )
|
2007-09-26 22:28:00 +00:00
|
|
|
add( -doc.body.offsetLeft, -doc.body.offsetTop );
|
2007-09-21 21:41:56 +00:00
|
|
|
|
|
|
|
// Add the document scroll offsets if position is fixed
|
|
|
|
if ( fixed )
|
2007-12-12 16:55:55 +00:00
|
|
|
add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
|
|
|
|
Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
|
2007-09-08 14:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an object with top and left properties
|
|
|
|
results = { top: top, left: left };
|
|
|
|
}
|
|
|
|
|
|
|
|
function border(elem) {
|
2007-12-19 19:05:34 +00:00
|
|
|
add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) );
|
2007-09-08 14:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function add(l, t) {
|
|
|
|
left += parseInt(l) || 0;
|
2007-09-26 22:28:00 +00:00
|
|
|
top += parseInt(t) || 0;
|
2007-09-08 14:44:29 +00:00
|
|
|
}
|
2007-12-16 01:03:50 +00:00
|
|
|
|
|
|
|
return results;
|
2007-09-08 14:44:29 +00:00
|
|
|
};
|
2008-03-15 18:53:40 +00:00
|
|
|
|
|
|
|
// Create innerHeight, innerWidth, outerHeight and outerWidth methods
|
|
|
|
jQuery.each(["Height", "Width"], function(i, name){
|
|
|
|
|
|
|
|
var tl = name == "Height" ? "Top" : "Left", // top or left
|
|
|
|
br = name == "Height" ? "Bottom" : "Right"; // bottom or right
|
|
|
|
|
|
|
|
// innerHeight and innerWidth
|
|
|
|
jQuery.fn["inner" + name] = function(){
|
|
|
|
return this[ name.toLowerCase() ]() +
|
|
|
|
num(this, "padding" + tl) +
|
|
|
|
num(this, "padding" + br);
|
|
|
|
};
|
|
|
|
|
|
|
|
// outerHeight and outerWidth
|
2008-03-15 19:00:07 +00:00
|
|
|
jQuery.fn["outer" + name] = function(margin) {
|
2008-03-15 18:53:40 +00:00
|
|
|
return this["inner" + name]() +
|
|
|
|
num(this, "border" + tl + "Width") +
|
|
|
|
num(this, "border" + br + "Width") +
|
2008-03-15 19:00:07 +00:00
|
|
|
(!!margin ?
|
|
|
|
num(this, "margin" + tl) + num(this, "margin" + br) : 0);
|
2008-03-15 18:53:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
function num(elem, prop) {
|
|
|
|
elem = elem.jquery ? elem[0] : elem;
|
|
|
|
return elem && parseInt( jQuery.curCSS(elem, prop, true) ) || 0;
|
|
|
|
}
|