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 ( !(mozilla && elem == document.body) && 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, 10) || 0;
95 top += parseInt(t, 10) || 0;
103 position: function() {
104 var left = 0, top = 0, results;
107 // Get *real* offsetParent
108 var offsetParent = this.offsetParent(),
110 // Get correct offsets
111 offset = this.offset(),
112 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
114 // Subtract element margins
115 // note: when an element has margin: auto the offsetLeft and marginLeft
116 // are the same in Safari causing offset.left to incorrectly be 0
117 offset.top -= num( this, 'marginTop' );
118 offset.left -= num( this, 'marginLeft' );
120 // Add offsetParent borders
121 parentOffset.top += num( offsetParent, 'borderTopWidth' );
122 parentOffset.left += num( offsetParent, 'borderLeftWidth' );
124 // Subtract the two offsets
126 top: offset.top - parentOffset.top,
127 left: offset.left - parentOffset.left
134 offsetParent: function() {
135 var offsetParent = this[0].offsetParent || document.body;
136 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
137 offsetParent = offsetParent.offsetParent;
138 return jQuery(offsetParent);
143 // Create scrollLeft and scrollTop methods
144 jQuery.each( ['Left', 'Top'], function(i, name) {
145 var method = 'scroll' + name;
147 jQuery.fn[ method ] = function(val) {
148 if (!this[0]) return;
150 return val != undefined ?
152 // Set the scroll offset
153 this.each(function() {
154 this == window || this == document ?
156 !i ? val : jQuery(window).scrollLeft(),
157 i ? val : jQuery(window).scrollTop()
159 this[ method ] = val;
162 // Return the scroll offset
163 this[0] == window || this[0] == document ?
164 self[ i ? 'pageYOffset' : 'pageXOffset' ] ||
165 jQuery.boxModel && document.documentElement[ method ] ||
166 document.body[ method ] :