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