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