fix for #4263, better support for nested fixed position elements
[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.initialized || 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                 if ( this.initialized ) return;
56                 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, prop, bodyMarginTop = body.style.marginTop,
57                         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>';
58
59                 jQuery.extend( container.style, { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' } );
60
61                 container.innerHTML = html;
62                 body.insertBefore(container, body.firstChild);
63                 innerDiv = container.firstChild, checkDiv = innerDiv.firstChild, td = innerDiv.nextSibling.firstChild.firstChild;
64
65                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
66                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
67
68                 checkDiv.style.position = 'fixed', checkDiv.style.top = '20px';
69                 this.supportsFixedPosition = (checkDiv.offsetTop >= 15); // safari subtracts parent border width here which is 5px
70                 checkDiv.style.position = '', checkDiv.style.top = '';
71
72                 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
73                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
74
75                 body.style.marginTop = '1px';
76                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop === 0);
77                 body.style.marginTop = bodyMarginTop;
78
79                 body.removeChild(container);
80                 this.initialized = true;
81         },
82
83         bodyOffset: function(body) {
84                 jQuery.offset.initialized || jQuery.offset.initialize();
85                 var top = body.offsetTop, left = body.offsetLeft;
86                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
87                         top  += parseFloat( jQuery.curCSS(body, 'marginTop',  true), 10 ) || 0,
88                         left += parseFloat( jQuery.curCSS(body, 'marginLeft', true), 10 ) || 0;
89                 return { top: top, left: left };
90         }
91 };
92
93
94 jQuery.fn.extend({
95         position: function() {
96                 if ( !this[0] ) return null;
97
98                 var elem = this[0], left = 0, top = 0, results,
99
100                 // Get *real* offsetParent
101                 offsetParent = this.offsetParent(),
102
103                 // Get correct offsets
104                 offset       = this.offset(),
105                 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
106
107                 // Subtract element margins
108                 // note: when an element has margin: auto the offsetLeft and marginLeft 
109                 // are the same in Safari causing offset.left to incorrectly be 0
110                 offset.top  -= parseFloat( jQuery.curCSS(elem, 'marginTop',  true), 10 ) || 0;
111                 offset.left -= parseFloat( jQuery.curCSS(elem, 'marginLeft', true), 10 ) || 0;
112
113                 // Add offsetParent borders
114                 parentOffset.top  += parseFloat( jQuery.curCSS(offsetParent[0], 'borderTopWidth',  true), 10 ) || 0;
115                 parentOffset.left += parseFloat( jQuery.curCSS(offsetParent[0], 'borderLeftWidth', true), 10 ) || 0;
116
117                 // Subtract the two offsets
118                 results = {
119                         top:  offset.top  - parentOffset.top,
120                         left: offset.left - parentOffset.left
121                 };
122
123                 return results;
124         },
125
126         offsetParent: function() {
127                 var offsetParent = this[0].offsetParent || document.body;
128                 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
129                         offsetParent = offsetParent.offsetParent;
130                 return jQuery(offsetParent);
131         }
132 });
133
134
135 // Create scrollLeft and scrollTop methods
136 jQuery.each( ['Left', 'Top'], function(i, name) {
137         var method = 'scroll' + name;
138         
139         jQuery.fn[ method ] = function(val) {
140                 if ( !this[0] ) return null;
141
142                 return val !== undefined ?
143
144                         // Set the scroll offset
145                         this.each(function() {
146                                 this == window || this == document ?
147                                         window.scrollTo(
148                                                 !i ? val : jQuery(window).scrollLeft(),
149                                                  i ? val : jQuery(window).scrollTop()
150                                         ) :
151                                         this[ method ] = val;
152                         }) :
153
154                         // Return the scroll offset
155                         this[0] == window || this[0] == document ?
156                                 self[ i ? 'pageYOffset' : 'pageXOffset' ] ||
157                                         jQuery.boxModel && document.documentElement[ method ] ||
158                                         document.body[ method ] :
159                                 this[0][ method ];
160         };
161 });