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