5 "./var/documentElement",
13 "./selector" // contains
14 ], function( jQuery, access, document, documentElement, rnumnonpx, curCSS, addGetHookIf, support ) {
17 * Gets a window from an element
19 function getWindow( elem ) {
20 return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView;
24 setOffset: function( elem, options, i ) {
25 var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
26 position = jQuery.css( elem, "position" ),
27 curElem = jQuery( elem ),
30 // Set position first, in-case top/left are set even on static elem
31 if ( position === "static" ) {
32 elem.style.position = "relative";
35 curOffset = curElem.offset();
36 curCSSTop = jQuery.css( elem, "top" );
37 curCSSLeft = jQuery.css( elem, "left" );
38 calculatePosition = ( position === "absolute" || position === "fixed" ) &&
39 ( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1;
41 // Need to be able to calculate position if either
42 // top or left is auto and position is either absolute or fixed
43 if ( calculatePosition ) {
44 curPosition = curElem.position();
45 curTop = curPosition.top;
46 curLeft = curPosition.left;
49 curTop = parseFloat( curCSSTop ) || 0;
50 curLeft = parseFloat( curCSSLeft ) || 0;
53 if ( jQuery.isFunction( options ) ) {
55 // Use jQuery.extend here to allow modification of coordinates argument (gh-1848)
56 options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
59 if ( options.top != null ) {
60 props.top = ( options.top - curOffset.top ) + curTop;
62 if ( options.left != null ) {
63 props.left = ( options.left - curOffset.left ) + curLeft;
66 if ( "using" in options ) {
67 options.using.call( elem, props );
76 offset: function( options ) {
77 if ( arguments.length ) {
78 return options === undefined ?
80 this.each( function( i ) {
81 jQuery.offset.setOffset( this, options, i );
87 box = { top: 0, left: 0 },
88 doc = elem && elem.ownerDocument;
94 docElem = doc.documentElement;
96 // Make sure it's not a disconnected DOM node
97 if ( !jQuery.contains( docElem, elem ) ) {
101 box = elem.getBoundingClientRect();
102 win = getWindow( doc );
104 top: box.top + win.pageYOffset - docElem.clientTop,
105 left: box.left + win.pageXOffset - docElem.clientLeft
109 position: function() {
114 var offsetParent, offset,
116 parentOffset = { top: 0, left: 0 };
118 // Fixed elements are offset from window (parentOffset = {top:0, left: 0},
119 // because it is its only offset parent
120 if ( jQuery.css( elem, "position" ) === "fixed" ) {
122 // Assume getBoundingClientRect is there when computed position is fixed
123 offset = elem.getBoundingClientRect();
127 // Get *real* offsetParent
128 offsetParent = this.offsetParent();
130 // Get correct offsets
131 offset = this.offset();
132 if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
133 parentOffset = offsetParent.offset();
136 // Add offsetParent borders
137 parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
138 parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
141 // Subtract parent offsets and element margins
143 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
144 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
148 // This method will return documentElement in the following cases:
149 // 1) For the element inside the iframe without offsetParent, this method will return
150 // documentElement of the parent window
151 // 2) For the hidden or detached element
152 // 3) For body or html element, i.e. in case of the html node - it will return itself
154 // but those exceptions were never presented as a real life use-cases
155 // and might be considered as more preferable results.
157 // This logic, however, is not guaranteed and can change at any point in the future
158 offsetParent: function() {
159 return this.map( function() {
160 var offsetParent = this.offsetParent;
162 while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
163 offsetParent = offsetParent.offsetParent;
166 return offsetParent || documentElement;
171 // Create scrollLeft and scrollTop methods
172 jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
173 var top = "pageYOffset" === prop;
175 jQuery.fn[ method ] = function( val ) {
176 return access( this, function( elem, method, val ) {
177 var win = getWindow( elem );
179 if ( val === undefined ) {
180 return win ? win[ prop ] : elem[ method ];
185 !top ? val : win.pageXOffset,
186 top ? val : win.pageYOffset
190 elem[ method ] = val;
192 }, method, val, arguments.length );
196 // Support: Safari<7-8+, Chrome<37-44+
197 // Add the top/left cssHooks using jQuery.fn.position
198 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
199 // Blink bug: https://code.google.com/p/chromium/issues/detail?id=229280
200 // getComputedStyle returns percent when specified for top/left/bottom/right;
201 // rather than make the css module depend on the offset module, just check for it here
202 jQuery.each( [ "top", "left" ], function( i, prop ) {
203 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
204 function( elem, computed ) {
206 computed = curCSS( elem, prop );
208 // If curCSS returns percentage, fallback to offset
209 return rnumnonpx.test( computed ) ?
210 jQuery( elem ).position()[ prop ] + "px" :