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