Fixed an issue with setting offset of absolutely positioned element that has no posit...
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement ) {
2         jQuery.fn.offset = function( options ) {
3                 var elem = this[0];
4
5                 if ( options ) { 
6                         return this.each(function( i ) {
7                                 jQuery.offset.setOffset( this, options, i );
8                         });
9                 }
10
11                 if ( !elem || !elem.ownerDocument ) {
12                         return null;
13                 }
14
15                 if ( elem === elem.ownerDocument.body ) {
16                         return jQuery.offset.bodyOffset( elem );
17                 }
18
19                 var box = elem.getBoundingClientRect(),
20                         doc = elem.ownerDocument,
21                         body = doc.body,
22                         docElem = doc.documentElement,
23                         win = getWindow(doc),
24                         clientTop  = docElem.clientTop  || body.clientTop  || 0,
25                         clientLeft = docElem.clientLeft || body.clientLeft || 0,
26                         scrollTop  = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ),
27                         scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
28                         top  = box.top  + scrollTop  - clientTop,
29                         left = box.left + scrollLeft - clientLeft;
30
31                 return { top: top, left: left };
32         };
33
34 } else {
35         jQuery.fn.offset = function( options ) {
36                 var elem = this[0];
37
38                 if ( options ) { 
39                         return this.each(function( i ) {
40                                 jQuery.offset.setOffset( this, options, i );
41                         });
42                 }
43
44                 if ( !elem || !elem.ownerDocument ) {
45                         return null;
46                 }
47
48                 if ( elem === elem.ownerDocument.body ) {
49                         return jQuery.offset.bodyOffset( elem );
50                 }
51
52                 jQuery.offset.initialize();
53
54                 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
55                         doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
56                         body = doc.body, defaultView = doc.defaultView,
57                         prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
58                         top = elem.offsetTop, left = elem.offsetLeft;
59
60                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
61                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
62                                 break;
63                         }
64
65                         computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
66                         top  -= elem.scrollTop;
67                         left -= elem.scrollLeft;
68
69                         if ( elem === offsetParent ) {
70                                 top  += elem.offsetTop;
71                                 left += elem.offsetLeft;
72
73                                 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.nodeName)) ) {
74                                         top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
75                                         left += parseFloat( computedStyle.borderLeftWidth ) || 0;
76                                 }
77
78                                 prevOffsetParent = offsetParent;
79                                 offsetParent = elem.offsetParent;
80                         }
81
82                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
83                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
84                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
85                         }
86
87                         prevComputedStyle = computedStyle;
88                 }
89
90                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
91                         top  += body.offsetTop;
92                         left += body.offsetLeft;
93                 }
94
95                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
96                         top  += Math.max( docElem.scrollTop, body.scrollTop );
97                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
98                 }
99
100                 return { top: top, left: left };
101         };
102 }
103
104 jQuery.offset = {
105         initialize: function() {
106                 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, "marginTop", true) ) || 0,
107                         html = "<div style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;'><div></div></div><table style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;' cellpadding='0' cellspacing='0'><tr><td></td></tr></table>";
108
109                 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
110
111                 container.innerHTML = html;
112                 body.insertBefore( container, body.firstChild );
113                 innerDiv = container.firstChild;
114                 checkDiv = innerDiv.firstChild;
115                 td = innerDiv.nextSibling.firstChild.firstChild;
116
117                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
118                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
119
120                 checkDiv.style.position = "fixed";
121                 checkDiv.style.top = "20px";
122
123                 // safari subtracts parent border width here which is 5px
124                 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
125                 checkDiv.style.position = checkDiv.style.top = "";
126
127                 innerDiv.style.overflow = "hidden";
128                 innerDiv.style.position = "relative";
129
130                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
131
132                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
133
134                 body.removeChild( container );
135                 body = container = innerDiv = checkDiv = table = td = null;
136                 jQuery.offset.initialize = jQuery.noop;
137         },
138
139         bodyOffset: function( body ) {
140                 var top = body.offsetTop, left = body.offsetLeft;
141
142                 jQuery.offset.initialize();
143
144                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
145                         top  += parseFloat( jQuery.curCSS(body, "marginTop",  true) ) || 0;
146                         left += parseFloat( jQuery.curCSS(body, "marginLeft", true) ) || 0;
147                 }
148
149                 return { top: top, left: left };
150         },
151         
152         setOffset: function( elem, options, i ) {
153                 var position = jQuery.curCSS( elem, "position" );
154
155                 // set position first, in-case top/left are set even on static elem
156                 if ( position === "static" ) {
157                         elem.style.position = "relative";
158                 }
159
160                 var curElem    = jQuery( elem ),
161                         curOffset  = curElem.offset(),
162                         curCSSTop  = jQuery.curCSS( elem, "top", true ),
163                         curCSSLeft = jQuery.curCSS( elem, "left", true ),
164                         calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
165                         props = {}, curPosition = {}, curTop, curLeft;
166
167                 // need to be able to calculate position if either top or left is auto and position is absolute
168                 if ( calculatePosition ) {
169                         curPosition = curElem.position();
170                 }
171
172                 curTop  = calculatePosition ? curPosition.top  : parseInt( curCSSTop,  10 ) || 0;
173                 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
174
175                 if ( jQuery.isFunction( options ) ) {
176                         options = options.call( elem, i, curOffset );
177                 }
178
179                 if (options.top != null) {
180                         props.top = (options.top - curOffset.top) + curTop;
181                 }
182                 if (options.left != null) {
183                         props.left = (options.left - curOffset.left) + curLeft;
184                 }
185                 
186                 if ( "using" in options ) {
187                         options.using.call( elem, props );
188                 } else {
189                         curElem.css( props );
190                 }
191         }
192 };
193
194
195 jQuery.fn.extend({
196         position: function() {
197                 if ( !this[0] ) {
198                         return null;
199                 }
200
201                 var elem = this[0],
202
203                 // Get *real* offsetParent
204                 offsetParent = this.offsetParent(),
205
206                 // Get correct offsets
207                 offset       = this.offset(),
208                 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
209
210                 // Subtract element margins
211                 // note: when an element has margin: auto the offsetLeft and marginLeft
212                 // are the same in Safari causing offset.left to incorrectly be 0
213                 offset.top  -= parseFloat( jQuery.curCSS(elem, "marginTop",  true) ) || 0;
214                 offset.left -= parseFloat( jQuery.curCSS(elem, "marginLeft", true) ) || 0;
215
216                 // Add offsetParent borders
217                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], "borderTopWidth",  true) ) || 0;
218                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], "borderLeftWidth", true) ) || 0;
219
220                 // Subtract the two offsets
221                 return {
222                         top:  offset.top  - parentOffset.top,
223                         left: offset.left - parentOffset.left
224                 };
225         },
226
227         offsetParent: function() {
228                 return this.map(function() {
229                         var offsetParent = this.offsetParent || document.body;
230                         while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
231                                 offsetParent = offsetParent.offsetParent;
232                         }
233                         return offsetParent;
234                 });
235         }
236 });
237
238
239 // Create scrollLeft and scrollTop methods
240 jQuery.each( ["Left", "Top"], function( i, name ) {
241         var method = "scroll" + name;
242
243         jQuery.fn[ method ] = function(val) {
244                 var elem = this[0], win;
245                 
246                 if ( !elem ) {
247                         return null;
248                 }
249
250                 if ( val !== undefined ) {
251                         // Set the scroll offset
252                         return this.each(function() {
253                                 win = getWindow( this );
254
255                                 if ( win ) {
256                                         win.scrollTo(
257                                                 !i ? val : jQuery(win).scrollLeft(),
258                                                  i ? val : jQuery(win).scrollTop()
259                                         );
260
261                                 } else {
262                                         this[ method ] = val;
263                                 }
264                         });
265                 } else {
266                         win = getWindow( elem );
267
268                         // Return the scroll offset
269                         return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
270                                 jQuery.support.boxModel && win.document.documentElement[ method ] ||
271                                         win.document.body[ method ] :
272                                 elem[ method ];
273                 }
274         };
275 });
276
277 function getWindow( elem ) {
278         return ("scrollTo" in elem && elem.document) ?
279                 elem :
280                 elem.nodeType === 9 ?
281                         elem.defaultView || elem.parentWindow :
282                         false;
283 }