1 if ( document.documentElement["getBoundingClientRect"] )
2 jQuery.fn.offset = function() {
3 if ( !this[0] ) return { top: 0, left: 0 };
4 if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] );
5 var box = this[0].getBoundingClientRect(), doc = this[0].ownerDocument, body = doc.body, docElem = doc.documentElement,
6 clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
7 top = box.top + (self.pageYOffset || jQuery.boxModel && docElem.scrollTop || body.scrollTop ) - clientTop,
8 left = box.left + (self.pageXOffset || jQuery.boxModel && docElem.scrollLeft || body.scrollLeft) - clientLeft;
9 return { top: top, left: left };
12 jQuery.fn.offset = function() {
13 if ( !this[0] ) return { top: 0, left: 0 };
14 if ( this[0] === this[0].ownerDocument.body ) return jQuery.offset.bodyOffset( this[0] );
15 jQuery.offset.initialized || jQuery.offset.initialize();
17 var elem = this[0], offsetParent = elem.offsetParent, prevOffsetParent = elem,
18 doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
19 body = doc.body, defaultView = doc.defaultView,
20 prevComputedStyle = defaultView.getComputedStyle(elem, null),
21 top = elem.offsetTop, left = elem.offsetLeft;
23 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
24 computedStyle = defaultView.getComputedStyle(elem, null);
25 top -= elem.scrollTop, left -= elem.scrollLeft;
26 if ( elem === offsetParent ) {
27 top += elem.offsetTop, left += elem.offsetLeft;
28 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && /^t(able|d|h)$/i.test(elem.tagName)) )
29 top += parseInt( computedStyle.borderTopWidth, 10) || 0,
30 left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
31 prevOffsetParent = offsetParent, offsetParent = elem.offsetParent;
33 if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" )
34 top += parseInt( computedStyle.borderTopWidth, 10) || 0,
35 left += parseInt( computedStyle.borderLeftWidth, 10) || 0;
36 prevComputedStyle = computedStyle;
39 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" )
40 top += body.offsetTop,
41 left += body.offsetLeft;
43 if ( prevComputedStyle.position === "fixed" )
44 top += Math.max(docElem.scrollTop, body.scrollTop),
45 left += Math.max(docElem.scrollLeft, body.scrollLeft);
47 return { top: top, left: left };
51 initialize: function() {
52 if ( this.initialized ) return;
53 var body = document.body, container = document.createElement('div'), innerDiv, checkDiv, table, td, rules, prop, bodyMarginTop = body.style.marginTop,
54 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>';
56 rules = { position: 'absolute', top: 0, left: 0, margin: 0, border: 0, width: '1px', height: '1px', visibility: 'hidden' };
57 for ( prop in rules ) container.style[prop] = rules[prop];
59 container.innerHTML = html;
60 body.insertBefore(container, body.firstChild);
61 innerDiv = container.firstChild, checkDiv = innerDiv.firstChild, td = innerDiv.nextSibling.firstChild.firstChild;
63 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
64 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
66 innerDiv.style.overflow = 'hidden', innerDiv.style.position = 'relative';
67 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
69 body.style.marginTop = '1px';
70 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop === 0);
71 body.style.marginTop = bodyMarginTop;
73 body.removeChild(container);
74 this.initialized = true;
77 bodyOffset: function(body) {
78 jQuery.offset.initialized || jQuery.offset.initialize();
79 var top = body.offsetTop, left = body.offsetLeft;
80 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset )
81 top += parseInt( jQuery.curCSS(body, 'marginTop', true), 10 ) || 0,
82 left += parseInt( jQuery.curCSS(body, 'marginLeft', true), 10 ) || 0;
83 return { top: top, left: left };
89 position: function() {
90 var left = 0, top = 0, results;
93 // Get *real* offsetParent
94 var offsetParent = this.offsetParent(),
96 // Get correct offsets
97 offset = this.offset(),
98 parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
100 // Subtract element margins
101 // note: when an element has margin: auto the offsetLeft and marginLeft
102 // are the same in Safari causing offset.left to incorrectly be 0
103 offset.top -= num( this, 'marginTop' );
104 offset.left -= num( this, 'marginLeft' );
106 // Add offsetParent borders
107 parentOffset.top += num( offsetParent, 'borderTopWidth' );
108 parentOffset.left += num( offsetParent, 'borderLeftWidth' );
110 // Subtract the two offsets
112 top: offset.top - parentOffset.top,
113 left: offset.left - parentOffset.left
120 offsetParent: function() {
121 var offsetParent = this[0].offsetParent || document.body;
122 while ( offsetParent && (!/^body|html$/i.test(offsetParent.tagName) && jQuery.css(offsetParent, 'position') == 'static') )
123 offsetParent = offsetParent.offsetParent;
124 return jQuery(offsetParent);
129 // Create scrollLeft and scrollTop methods
130 jQuery.each( ['Left', 'Top'], function(i, name) {
131 var method = 'scroll' + name;
133 jQuery.fn[ method ] = function(val) {
134 if (!this[0]) return null;
136 return val !== undefined ?
138 // Set the scroll offset
139 this.each(function() {
140 this == window || this == document ?
142 !i ? val : jQuery(window).scrollLeft(),
143 i ? val : jQuery(window).scrollTop()
145 this[ method ] = val;
148 // Return the scroll offset
149 this[0] == window || this[0] == document ?
150 self[ i ? 'pageYOffset' : 'pageXOffset' ] ||
151 jQuery.boxModel && document.documentElement[ method ] ||
152 document.body[ method ] :