5 "./var/documentElement",
14 "./selector" // contains
15 ], function( jQuery, access, document, documentElement, rnumnonpx,
16 curCSS, addGetHookIf, support, nodeName ) {
21 setOffset: function( elem, options, i ) {
22 var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
23 position = jQuery.css( elem, "position" ),
24 curElem = jQuery( elem ),
27 // Set position first, in-case top/left are set even on static elem
28 if ( position === "static" ) {
29 elem.style.position = "relative";
32 curOffset = curElem.offset();
33 curCSSTop = jQuery.css( elem, "top" );
34 curCSSLeft = jQuery.css( elem, "left" );
35 calculatePosition = ( position === "absolute" || position === "fixed" ) &&
36 ( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1;
38 // Need to be able to calculate position if either
39 // top or left is auto and position is either absolute or fixed
40 if ( calculatePosition ) {
41 curPosition = curElem.position();
42 curTop = curPosition.top;
43 curLeft = curPosition.left;
46 curTop = parseFloat( curCSSTop ) || 0;
47 curLeft = parseFloat( curCSSLeft ) || 0;
50 if ( jQuery.isFunction( options ) ) {
52 // Use jQuery.extend here to allow modification of coordinates argument (gh-1848)
53 options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
56 if ( options.top != null ) {
57 props.top = ( options.top - curOffset.top ) + curTop;
59 if ( options.left != null ) {
60 props.left = ( options.left - curOffset.left ) + curLeft;
63 if ( "using" in options ) {
64 options.using.call( elem, props );
73 offset: function( options ) {
75 // Preserve chaining for setter
76 if ( arguments.length ) {
77 return options === undefined ?
79 this.each( function( i ) {
80 jQuery.offset.setOffset( this, options, i );
84 var doc, docElem, rect, win,
91 // Return zeros for disconnected and hidden (display: none) elements (gh-2310)
92 // Support: IE <=11 only
93 // Running getBoundingClientRect on a
94 // disconnected node in IE throws an error
95 if ( !elem.getClientRects().length ) {
96 return { top: 0, left: 0 };
99 rect = elem.getBoundingClientRect();
101 doc = elem.ownerDocument;
102 docElem = doc.documentElement;
103 win = doc.defaultView;
106 top: rect.top + win.pageYOffset - docElem.clientTop,
107 left: rect.left + win.pageXOffset - docElem.clientLeft
111 position: function() {
116 var offsetParent, offset,
118 parentOffset = { top: 0, left: 0 };
120 // Fixed elements are offset from window (parentOffset = {top:0, left: 0},
121 // because it is its only offset parent
122 if ( jQuery.css( elem, "position" ) === "fixed" ) {
124 // Assume getBoundingClientRect is there when computed position is fixed
125 offset = elem.getBoundingClientRect();
129 // Get *real* offsetParent
130 offsetParent = this.offsetParent();
132 // Get correct offsets
133 offset = this.offset();
134 if ( !nodeName( offsetParent[ 0 ], "html" ) ) {
135 parentOffset = offsetParent.offset();
138 // Add offsetParent borders
140 top: parentOffset.top + jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ),
141 left: parentOffset.left + jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true )
145 // Subtract parent offsets and element margins
147 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
148 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
152 // This method will return documentElement in the following cases:
153 // 1) For the element inside the iframe without offsetParent, this method will return
154 // documentElement of the parent window
155 // 2) For the hidden or detached element
156 // 3) For body or html element, i.e. in case of the html node - it will return itself
158 // but those exceptions were never presented as a real life use-cases
159 // and might be considered as more preferable results.
161 // This logic, however, is not guaranteed and can change at any point in the future
162 offsetParent: function() {
163 return this.map( function() {
164 var offsetParent = this.offsetParent;
166 while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
167 offsetParent = offsetParent.offsetParent;
170 return offsetParent || documentElement;
175 // Create scrollLeft and scrollTop methods
176 jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
177 var top = "pageYOffset" === prop;
179 jQuery.fn[ method ] = function( val ) {
180 return access( this, function( elem, method, val ) {
182 // Coalesce documents and windows
184 if ( jQuery.isWindow( elem ) ) {
186 } else if ( elem.nodeType === 9 ) {
187 win = elem.defaultView;
190 if ( val === undefined ) {
191 return win ? win[ prop ] : elem[ method ];
196 !top ? val : win.pageXOffset,
197 top ? val : win.pageYOffset
201 elem[ method ] = val;
203 }, method, val, arguments.length );
207 // Support: Safari <=7 - 9.1, Chrome <=37 - 49
208 // Add the top/left cssHooks using jQuery.fn.position
209 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
210 // Blink bug: https://bugs.chromium.org/p/chromium/issues/detail?id=589347
211 // getComputedStyle returns percent when specified for top/left/bottom/right;
212 // rather than make the css module depend on the offset module, just check for it here
213 jQuery.each( [ "top", "left" ], function( i, prop ) {
214 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
215 function( elem, computed ) {
217 computed = curCSS( elem, prop );
219 // If curCSS returns percentage, fallback to offset
220 return rnumnonpx.test( computed ) ?
221 jQuery( elem ).position()[ prop ] + "px" :