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