Fix for animating hyphenated CSS properties. Fixes #4156.
[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.nodeName)) ) {
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                 body = container = innerDiv = checkDiv = table = td = null;
100                 jQuery.offset.initialize = function(){};
101         },
102
103         bodyOffset: function(body) {
104                 var top = body.offsetTop, left = body.offsetLeft;
105
106                 jQuery.offset.initialize();
107
108                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
109                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true) ) || 0;
110                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true) ) || 0;
111                 }
112
113                 return { top: top, left: left };
114         }
115 };
116
117
118 jQuery.fn.extend({
119         position: function() {
120                 if ( !this[0] ) { return null; }
121
122                 var elem = this[0],
123
124                 // Get *real* offsetParent
125                 offsetParent = this.offsetParent(),
126
127                 // Get correct offsets
128                 offset       = this.offset(),
129                 parentOffset = /^body|html$/i.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
130
131                 // Subtract element margins
132                 // note: when an element has margin: auto the offsetLeft and marginLeft
133                 // are the same in Safari causing offset.left to incorrectly be 0
134                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true) ) || 0;
135                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true) ) || 0;
136
137                 // Add offsetParent borders
138                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true) ) || 0;
139                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true) ) || 0;
140
141                 // Subtract the two offsets
142                 return {
143                         top:  offset.top  - parentOffset.top,
144                         left: offset.left - parentOffset.left
145                 };
146         },
147
148         offsetParent: function() {
149                 return this.map(function(){
150                         var offsetParent = this.offsetParent || document.body;
151                         while ( offsetParent && (!/^body|html$/i.test(offsetParent.nodeName) && jQuery.css(offsetParent, 'position') === 'static') ) {
152                                 offsetParent = offsetParent.offsetParent;
153                         }
154                         return offsetParent;
155                 });
156         }
157 });
158
159
160 // Create scrollLeft and scrollTop methods
161 jQuery.each( ['Left', 'Top'], function(i, name) {
162         var method = 'scroll' + name;
163
164         jQuery.fn[ method ] = function(val) {
165                 var elem = this[0], win;
166                 
167                 if ( !elem ) { return null; }
168
169                 if ( val !== undefined ) {
170                         // Set the scroll offset
171                         return this.each(function() {
172                                 win = getWindow( this );
173
174                                 win ?
175                                         win.scrollTo(
176                                                 !i ? val : jQuery(win).scrollLeft(),
177                                                  i ? val : jQuery(win).scrollTop()
178                                         ) :
179                                         this[ method ] = val;
180                         });
181                 } else {
182                         win = getWindow( elem );
183
184                         // Return the scroll offset
185                         return win ? ('pageXOffset' in win) ? win[ i ? 'pageYOffset' : 'pageXOffset' ] :
186                                 jQuery.support.boxModel && win.document.documentElement[ method ] ||
187                                         win.document.body[ method ] :
188                                 elem[ method ];
189                 }
190         };
191 });
192
193 function getWindow( elem ) {
194         return ("scrollTo" in elem && elem.document) ?
195                 elem :
196                 elem.nodeType === 9 ?
197                         elem.defaultView || elem.parentWindow :
198                         false;
199 }