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" ) {
121 // Assume getBoundingClientRect is there when computed position is fixed
122 offset = elem.getBoundingClientRect();
125 // Get *real* offsetParent
126 offsetParent = this.offsetParent();
128 // Get correct offsets
129 offset = this.offset();
130 if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
131 parentOffset = offsetParent.offset();
134 // Add offsetParent borders
135 parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
136 parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
139 // Subtract parent offsets and element margins
141 top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
142 left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
146 // This method will return documentElement in the following cases:
147 // 1) For the element inside the iframe without offsetParent, this method will return
148 // documentElement of the parent window
149 // 2) For the hidden or detached element
150 // 3) For body or html element, i.e. in case of the html node - it will return itself
152 // but those exceptions were never presented as a real life use-cases
153 // and might be considered as more preferable results.
155 // This logic, however, is not guaranteed and can change at any point in the future
156 offsetParent: function() {
157 return this.map(function() {
158 var offsetParent = this.offsetParent;
160 while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
161 offsetParent = offsetParent.offsetParent;
164 return offsetParent || documentElement;
169 // Create scrollLeft and scrollTop methods
170 jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
171 var top = "pageYOffset" === prop;
173 jQuery.fn[ method ] = function( val ) {
174 return access( this, function( elem, method, val ) {
175 var win = getWindow( elem );
177 if ( val === undefined ) {
178 return win ? win[ prop ] : elem[ method ];
183 !top ? val : win.pageXOffset,
184 top ? val : win.pageYOffset
188 elem[ method ] = val;
190 }, method, val, arguments.length, null );
194 // Support: Safari<7+, Chrome<37+
195 // Add the top/left cssHooks using jQuery.fn.position
196 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
197 // Blink bug: https://code.google.com/p/chromium/issues/detail?id=229280
198 // getComputedStyle returns percent when specified for top/left/bottom/right;
199 // rather than make the css module depend on the offset module, just check for it here
200 jQuery.each( [ "top", "left" ], function( i, prop ) {
201 jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
202 function( elem, computed ) {
204 computed = curCSS( elem, prop );
205 // If curCSS returns percentage, fallback to offset
206 return rnumnonpx.test( computed ) ?
207 jQuery( elem ).position()[ prop ] + "px" :