Rewrote .offsetParent() to work against the full jQuery set, added tests. Fixes ...
[jquery.git] / src / offset.js
1 if ( "getBoundingClientRect" in document.documentElement ) {
2         jQuery.fn.offset = function() {
3                 var elem = this[0];
4                 if ( !elem || !elem.ownerDocument ) { return null; }
5                 if ( elem === elem.ownerDocument.body ) {
6                         return jQuery.offset.bodyOffset( elem );
7                 }
8
9                 var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement,
10                         clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
11                         top  = box.top  + (self.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ) - clientTop,
12                         left = box.left + (self.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
13                 return { top: top, left: left };
14         };
15 } else {
16         jQuery.fn.offset = function() {
17                 var elem = this[0];
18                 if ( !elem || !elem.ownerDocument ) { return null; }
19                 if ( elem === elem.ownerDocument.body ) {
20                         return jQuery.offset.bodyOffset( elem );
21                 }
22
23                 jQuery.offset.initialize();
24
25                 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
26                         doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
27                         body = doc.body, defaultView = doc.defaultView,
28                         prevComputedStyle = defaultView.getComputedStyle(elem, null),
29                         top = elem.offsetTop, left = elem.offsetLeft;
30
31                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
32                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) { break; }
33
34                         computedStyle = defaultView.getComputedStyle(elem, null);
35                         top -= elem.scrollTop;
36                         left -= elem.scrollLeft;
37
38                         if ( elem === offsetParent ) {
39                                 top += elem.offsetTop;
40                                 left += elem.offsetLeft;
41
42                                 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) ) {
43                                         top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
44                                         left += parseFloat( computedStyle.borderLeftWidth ) || 0;
45                                 }
46
47                                 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
48                         }
49
50                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
51                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
52                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
53                         }
54
55                         prevComputedStyle = computedStyle;
56                 }
57
58                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
59                         top  += body.offsetTop;
60                         left += body.offsetLeft;
61                 }
62
63                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
64                         top  += Math.max( docElem.scrollTop, body.scrollTop );
65                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
66                 }
67
68                 return { top: top, left: left };
69         };
70 }
71
72 jQuery.offset = {
73         initialize: function() {
74                 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.curCSS(body, 'marginTop', true) ) || 0,
75                         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>';
76
77                 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
78
79                 container.innerHTML = html;
80                 body.insertBefore( container, body.firstChild );
81                 innerDiv = container.firstChild;
82                 checkDiv = innerDiv.firstChild;
83                 td = innerDiv.nextSibling.firstChild.firstChild;
84
85                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
86                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
87
88                 checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
89                 // safari subtracts parent border width here which is 5px
90                 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
91                 checkDiv.style.position = checkDiv.style.top = '';
92
93                 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
94                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
95
96                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
97
98                 body.removeChild( container );
99                 jQuery.offset.initialize = function(){};
100
101                 body = container = innerDiv = checkDiv = table = td = null;
102         },
103
104         bodyOffset: function(body) {
105                 var top = body.offsetTop, left = body.offsetLeft;
106
107                 jQuery.offset.initialize();
108
109                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
110                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0;
111                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
112                 }
113
114                 return { top: top, left: left };
115         }
116 };
117
118
119 jQuery.fn.extend({
120         position: function() {
121                 if ( !this[0] ) { return null; }
122
123                 var elem = this[0],
124
125                 // Get *real* offsetParent
126                 offsetParent = this.offsetParent(),
127
128                 // Get correct offsets
129                 offset       = this.offset(),
130                 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
131
132                 // Subtract element margins
133                 // note: when an element has margin: auto the offsetLeft and marginLeft
134                 // are the same in Safari causing offset.left to incorrectly be 0
135                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true) ) || 0;
136                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
137
138                 // Add offsetParent borders
139                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true) ) || 0;
140                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
141
142                 // Subtract the two offsets
143                 return {
144                         top:  offset.top  - parentOffset.top,
145                         left: offset.left - parentOffset.left
146                 };
147         },
148
149         offsetParent: function() {
150                 return this.map(function(){
151                         var offsetParent = this.offsetParent || document.body;
152                         while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') === 'static') ) {
153                                 offsetParent = offsetParent.offsetParent;
154                         }
155                         return offsetParent;
156                 });
157         }
158 });
159
160
161 // Create scrollLeft and scrollTop methods
162 jQuery.each( ['Left', 'Top'], function(i, name) {
163         var method = 'scroll' + name;
164
165         jQuery.fn[ method ] = function(val) {
166                 if ( !this[0] ) { return null; }
167
168                 if ( val !== undefined ) {
169                         // Set the scroll offset
170                         return this.each(function() {
171                                 win = getWindow( this );
172
173                                 if ( win ) {
174                                         win.scrollTo(
175                                                 !i ? val : jQuery(win).scrollLeft(),
176                                                  i ? val : jQuery(win).scrollTop()
177                                         );
178                                 } else {
179                                         this[ method ] = val;
180                                 }
181                         });
182                 } else {
183                         var elem = this[0],
184                                 win  = getWindow( elem );
185
186                         // Return the scroll offset
187                         return win && 'pageXOffset' in win ?
188                                 win[ i ? 'pageYOffset' : 'pageXOffset' ] ||
189                                         jQuery.support.boxModel && win.document.documentElement[ method ] ||
190                                         win.document.body[ method ] :
191                                 elem[ method ];
192                 }
193         };
194
195         function getWindow( elem ) {
196                 return ("scrollTo" in elem && elem.document) ?
197                         elem :
198                         elem.nodeType === 9 ?
199                                 elem.defaultView || elem.parentWindow :
200                                 false;
201         }
202 });