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;
103 position: function() {
104 var left = 0, top = 0, elem = this[0], offset, parentOffset, offsetParent, results;
107 // Get *real* offsetParent
108 offsetParent = this.offsetParent();
110 // Get correct offsets
111 offset = this.offset();
112 parentOffset = offsetParent.offset();
114 // Subtract element margins
115 offset.top -= parseInt( jQuery.curCSS(elem, 'marginTop', true) ) || 0;
116 offset.left -= parseInt( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
118 // Add offsetParent borders
119 parentOffset.top += parseInt( jQuery.curCSS(offsetParent[0], 'borderTopWidth', true) ) || 0;
120 parentOffset.left += parseInt( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
122 // Subtract the two offsets
124 top: offset.top - parentOffset.top,
125 left: offset.left - parentOffset.left
132 offsetParent: function() {
133 var offsetParent = this[0].offsetParent;
134 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
135 offsetParent = offsetParent.offsetParent;
136 return jQuery(offsetParent);
141 // Create scrollLeft and scrollTop methods
142 jQuery.each( ['Left', 'Top'], function(i, name) {
143 jQuery.fn[ 'scroll' + name ] = function(val) {
144 if (!this[0]) return;
146 return val != undefined ?
148 // Set the scroll offset
149 this.each(function() {
150 this == window || this == document ?
152 name == 'Left' ? val : jQuery(window)[ 'scrollLeft' ](),
153 name == 'Top' ? val : jQuery(window)[ 'scrollTop' ]()
155 this[ 'scroll' + name ] = val;
158 // Return the scroll offset
159 this[0] == window || this[0] == document ?
160 self[ (name == 'Left' ? 'pageXOffset' : 'pageYOffset') ] ||
161 jQuery.boxModel && document.documentElement[ 'scroll' + name ] ||
162 document.body[ 'scroll' + name ] :
163 this[0][ 'scroll' + name ];