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