Blame view
app/bower_components/jquery/src/offset.js
6.2 KB
|
87c93a029
|
1 |
define( [ |
|
f986e111b
|
2 |
"./core", |
|
f986e111b
|
3 |
"./core/access", |
|
87c93a029
|
4 5 |
"./var/document", "./var/documentElement", |
|
f986e111b
|
6 7 8 9 10 11 12 13 |
"./css/var/rnumnonpx", "./css/curCSS", "./css/addGetHookIf", "./css/support", "./core/init", "./css", "./selector" // contains |
|
87c93a029
|
14 |
], function( jQuery, access, document, documentElement, rnumnonpx, curCSS, addGetHookIf, support ) {
|
|
f986e111b
|
15 |
|
|
87c93a029
|
16 |
"use strict"; |
|
f986e111b
|
17 18 19 20 21 |
/**
* Gets a window from an element
*/
function getWindow( elem ) {
|
|
87c93a029
|
22 |
return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView; |
|
f986e111b
|
23 24 25 26 27 28 29 30 |
}
jQuery.offset = {
setOffset: function( elem, options, i ) {
var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
position = jQuery.css( elem, "position" ),
curElem = jQuery( elem ),
props = {};
|
|
87c93a029
|
31 |
// Set position first, in-case top/left are set even on static elem |
|
f986e111b
|
32 33 34 35 36 37 38 39 |
if ( position === "static" ) {
elem.style.position = "relative";
}
curOffset = curElem.offset();
curCSSTop = jQuery.css( elem, "top" );
curCSSLeft = jQuery.css( elem, "left" );
calculatePosition = ( position === "absolute" || position === "fixed" ) &&
|
|
87c93a029
|
40 |
( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1; |
|
f986e111b
|
41 |
|
|
87c93a029
|
42 43 |
// Need to be able to calculate position if either // top or left is auto and position is either absolute or fixed |
|
f986e111b
|
44 45 46 47 |
if ( calculatePosition ) {
curPosition = curElem.position();
curTop = curPosition.top;
curLeft = curPosition.left;
|
|
87c93a029
|
48 |
|
|
f986e111b
|
49 50 51 52 53 54 |
} else {
curTop = parseFloat( curCSSTop ) || 0;
curLeft = parseFloat( curCSSLeft ) || 0;
}
if ( jQuery.isFunction( options ) ) {
|
|
87c93a029
|
55 56 57 |
// Use jQuery.extend here to allow modification of coordinates argument (gh-1848)
options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
|
|
f986e111b
|
58 59 60 61 62 63 64 65 66 67 68 |
}
if ( options.top != null ) {
props.top = ( options.top - curOffset.top ) + curTop;
}
if ( options.left != null ) {
props.left = ( options.left - curOffset.left ) + curLeft;
}
if ( "using" in options ) {
options.using.call( elem, props );
|
|
87c93a029
|
69 |
|
|
f986e111b
|
70 71 72 73 74 |
} else {
curElem.css( props );
}
}
};
|
|
87c93a029
|
75 |
jQuery.fn.extend( {
|
|
f986e111b
|
76 |
offset: function( options ) {
|
|
87c93a029
|
77 78 |
// Preserve chaining for setter |
|
f986e111b
|
79 80 81 |
if ( arguments.length ) {
return options === undefined ?
this :
|
|
87c93a029
|
82 |
this.each( function( i ) {
|
|
f986e111b
|
83 |
jQuery.offset.setOffset( this, options, i ); |
|
87c93a029
|
84 |
} ); |
|
f986e111b
|
85 |
} |
|
87c93a029
|
86 87 |
var docElem, win, rect, doc, elem = this[ 0 ]; |
|
f986e111b
|
88 |
|
|
87c93a029
|
89 |
if ( !elem ) {
|
|
f986e111b
|
90 91 |
return; } |
|
87c93a029
|
92 93 94 95 96 |
// Support: IE <=11 only
// Running getBoundingClientRect on a
// disconnected node in IE throws an error
if ( !elem.getClientRects().length ) {
return { top: 0, left: 0 };
|
|
f986e111b
|
97 |
} |
|
87c93a029
|
98 99 100 101 102 103 104 105 106 107 108 109 |
rect = elem.getBoundingClientRect();
// Make sure element is not hidden (display: none)
if ( rect.width || rect.height ) {
doc = elem.ownerDocument;
win = getWindow( doc );
docElem = doc.documentElement;
return {
top: rect.top + win.pageYOffset - docElem.clientTop,
left: rect.left + win.pageXOffset - docElem.clientLeft
};
|
|
f986e111b
|
110 |
} |
|
87c93a029
|
111 112 113 |
// Return zeros for disconnected and hidden elements (gh-2310) return rect; |
|
f986e111b
|
114 115 116 117 118 119 120 121 |
},
position: function() {
if ( !this[ 0 ] ) {
return;
}
var offsetParent, offset,
|
|
87c93a029
|
122 123 |
elem = this[ 0 ],
parentOffset = { top: 0, left: 0 };
|
|
f986e111b
|
124 |
|
|
87c93a029
|
125 126 |
// Fixed elements are offset from window (parentOffset = {top:0, left: 0},
// because it is its only offset parent
|
|
f986e111b
|
127 |
if ( jQuery.css( elem, "position" ) === "fixed" ) {
|
|
87c93a029
|
128 129 |
// Assume getBoundingClientRect is there when computed position is fixed |
|
f986e111b
|
130 |
offset = elem.getBoundingClientRect(); |
|
87c93a029
|
131 |
|
|
f986e111b
|
132 |
} else {
|
|
87c93a029
|
133 |
|
|
f986e111b
|
134 135 136 137 138 139 140 141 142 143 |
// Get *real* offsetParent
offsetParent = this.offsetParent();
// Get correct offsets
offset = this.offset();
if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
parentOffset = offsetParent.offset();
}
// Add offsetParent borders
|
|
87c93a029
|
144 145 146 147 |
parentOffset = {
top: parentOffset.top + jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ),
left: parentOffset.left + jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true )
};
|
|
f986e111b
|
148 149 150 |
} // Subtract parent offsets and element margins |
|
f986e111b
|
151 |
return {
|
|
87c93a029
|
152 153 |
top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ), left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true ) |
|
f986e111b
|
154 155 |
}; }, |
|
87c93a029
|
156 157 158 159 160 161 162 163 164 165 |
// This method will return documentElement in the following cases: // 1) For the element inside the iframe without offsetParent, this method will return // documentElement of the parent window // 2) For the hidden or detached element // 3) For body or html element, i.e. in case of the html node - it will return itself // // but those exceptions were never presented as a real life use-cases // and might be considered as more preferable results. // // This logic, however, is not guaranteed and can change at any point in the future |
|
f986e111b
|
166 |
offsetParent: function() {
|
|
87c93a029
|
167 168 |
return this.map( function() {
var offsetParent = this.offsetParent;
|
|
f986e111b
|
169 |
|
|
87c93a029
|
170 |
while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
|
|
f986e111b
|
171 172 |
offsetParent = offsetParent.offsetParent; } |
|
87c93a029
|
173 174 175 |
return offsetParent || documentElement; } ); |
|
f986e111b
|
176 |
} |
|
87c93a029
|
177 |
} ); |
|
f986e111b
|
178 179 180 |
// Create scrollLeft and scrollTop methods
jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
|
|
87c93a029
|
181 |
var top = "pageYOffset" === prop; |
|
f986e111b
|
182 183 184 185 186 187 |
jQuery.fn[ method ] = function( val ) {
return access( this, function( elem, method, val ) {
var win = getWindow( elem );
if ( val === undefined ) {
|
|
87c93a029
|
188 |
return win ? win[ prop ] : elem[ method ]; |
|
f986e111b
|
189 190 191 192 |
}
if ( win ) {
win.scrollTo(
|
|
87c93a029
|
193 194 |
!top ? val : win.pageXOffset, top ? val : win.pageYOffset |
|
f986e111b
|
195 196 197 198 199 |
);
} else {
elem[ method ] = val;
}
|
|
87c93a029
|
200 |
}, method, val, arguments.length ); |
|
f986e111b
|
201 |
}; |
|
87c93a029
|
202 |
} ); |
|
f986e111b
|
203 |
|
|
87c93a029
|
204 |
// Support: Safari <=7 - 9.1, Chrome <=37 - 49 |
|
f986e111b
|
205 206 |
// Add the top/left cssHooks using jQuery.fn.position // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084 |
|
87c93a029
|
207 208 209 |
// Blink bug: https://bugs.chromium.org/p/chromium/issues/detail?id=589347 // getComputedStyle returns percent when specified for top/left/bottom/right; // rather than make the css module depend on the offset module, just check for it here |
|
f986e111b
|
210 211 212 213 214 |
jQuery.each( [ "top", "left" ], function( i, prop ) {
jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
function( elem, computed ) {
if ( computed ) {
computed = curCSS( elem, prop );
|
|
87c93a029
|
215 216 |
// If curCSS returns percentage, fallback to offset |
|
f986e111b
|
217 218 219 220 221 222 |
return rnumnonpx.test( computed ) ? jQuery( elem ).position()[ prop ] + "px" : computed; } } ); |
|
87c93a029
|
223 |
} ); |
|
f986e111b
|
224 225 |
return jQuery; |
|
87c93a029
|
226 |
} ); |