2 // Originally By Brandon Aaron, part of the Dimension Plugin
3 // http://jquery.com/plugins/project/dimensions
4 jQuery.fn.offset = function() {
5 var left = 0, top = 0, elem = this[0], results;
7 if ( elem ) with ( jQuery.browser ) {
8 var parent = elem.parentNode,
10 offsetParent = elem.offsetParent,
11 doc = elem.ownerDocument,
12 safari2 = safari && parseInt(version) < 522 && !/adobeair/i.test(userAgent),
14 fixed = css(elem, "position") == "fixed";
16 // Use getBoundingClientRect if available
17 if ( elem.getBoundingClientRect ) {
18 var box = elem.getBoundingClientRect();
20 // Add the document scroll offsets
21 add(box.left + Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
22 box.top + Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
24 // IE adds the HTML element's border, by default it is medium which is 2px
25 // IE 6 and 7 quirks mode the border width is overwritable by the following css html { border: 0; }
26 // IE 7 standards mode, the border is always 2px
27 // This border/offset is typically represented by the clientLeft and clientTop properties
28 // However, in IE6 and 7 quirks mode the clientLeft and clientTop properties are not updated when overwriting it via CSS
29 // Therefore this method will be off by 2px in IE while in quirksmode
30 add( -doc.documentElement.clientLeft, -doc.documentElement.clientTop );
32 // Otherwise loop through the offsetParents and parentNodes
35 // Initial element offsets
36 add( elem.offsetLeft, elem.offsetTop );
39 while ( offsetParent ) {
40 // Add offsetParent offsets
41 add( offsetParent.offsetLeft, offsetParent.offsetTop );
43 // Mozilla and Safari > 2 does not include the border on offset parents
44 // However Mozilla adds the border for table or table cells
45 if ( mozilla && !/^t(able|d|h)$/i.test(offsetParent.tagName) || safari && !safari2 )
46 border( offsetParent );
48 // Add the document scroll offsets if position is fixed on any offsetParent
49 if ( !fixed && css(offsetParent, "position") == "fixed" )
52 // Set offsetChild to previous offsetParent unless it is the body element
53 offsetChild = /^body$/i.test(offsetParent.tagName) ? offsetChild : offsetParent;
54 // Get next offsetParent
55 offsetParent = offsetParent.offsetParent;
58 // Get parent scroll offsets
59 while ( parent && parent.tagName && !/^body|html$/i.test(parent.tagName) ) {
60 // Remove parent scroll UNLESS that parent is inline or a table to work around Opera inline/table scrollLeft/Top bug
61 if ( !/^inline|table.*$/i.test(css(parent, "display")) )
62 // Subtract parent scroll offsets
63 add( -parent.scrollLeft, -parent.scrollTop );
65 // Mozilla does not add the border for a parent that has overflow != visible
66 if ( mozilla && css(parent, "overflow") != "visible" )
70 parent = parent.parentNode;
73 // Safari <= 2 doubles body offsets with a fixed position element/offsetParent or absolutely positioned offsetChild
74 // Mozilla doubles body offsets with a non-absolutely positioned offsetChild
75 if ( (safari2 && (fixed || css(offsetChild, "position") == "absolute")) ||
76 (mozilla && css(offsetChild, "position") != "absolute") )
77 add( -doc.body.offsetLeft, -doc.body.offsetTop );
79 // Add the document scroll offsets if position is fixed
81 add(Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
82 Math.max(doc.documentElement.scrollTop, doc.body.scrollTop));
85 // Return an object with top and left properties
86 results = { top: top, left: left };
89 function border(elem) {
90 add( jQuery.curCSS(elem, "borderLeftWidth", true), jQuery.curCSS(elem, "borderTopWidth", true) );
94 left += parseInt(l) || 0;
95 top += parseInt(t) || 0;
101 // Create innerHeight, innerWidth, outerHeight and outerWidth methods
102 jQuery.each(["Height", "Width"], function(i, name){
104 var tl = name == "Height" ? "Top" : "Left", // top or left
105 br = name == "Height" ? "Bottom" : "Right"; // bottom or right
107 // innerHeight and innerWidth
108 jQuery.fn["inner" + name] = function(){
109 return this[ name.toLowerCase() ]() +
110 num(this, "padding" + tl) +
111 num(this, "padding" + br);
114 // outerHeight and outerWidth
115 jQuery.fn["outer" + name] = function(margin) {
116 return this["inner" + name]() +
117 num(this, "border" + tl + "Width") +
118 num(this, "border" + br + "Width") +
120 num(this, "margin" + tl) + num(this, "margin" + br) : 0);
125 function num(elem, prop) {
126 elem = elem.jquery ? elem[0] : elem;
127 return elem && parseInt( jQuery.curCSS(elem, prop, true) ) || 0;