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