ffffng/app/bower_components/jquery/src/offset.js

230 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-05-13 13:25:33 +02:00
define( [
2014-05-12 20:08:19 +02:00
"./core",
"./core/access",
2017-05-13 13:25:33 +02:00
"./var/document",
"./var/documentElement",
2014-05-12 20:08:19 +02:00
"./css/var/rnumnonpx",
"./css/curCSS",
"./css/addGetHookIf",
"./css/support",
2017-05-13 13:25:33 +02:00
"./core/nodeName",
2014-05-12 20:08:19 +02:00
"./core/init",
"./css",
"./selector" // contains
2017-05-13 13:25:33 +02:00
], function( jQuery, access, document, documentElement, rnumnonpx,
curCSS, addGetHookIf, support, nodeName ) {
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
"use strict";
2014-05-12 20:08:19 +02:00
jQuery.offset = {
setOffset: function( elem, options, i ) {
var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
position = jQuery.css( elem, "position" ),
curElem = jQuery( elem ),
props = {};
// Set position first, in-case top/left are set even on static elem
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" ) &&
2017-05-13 13:25:33 +02:00
( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1;
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
// Need to be able to calculate position if either
// top or left is auto and position is either absolute or fixed
2014-05-12 20:08:19 +02:00
if ( calculatePosition ) {
curPosition = curElem.position();
curTop = curPosition.top;
curLeft = curPosition.left;
} else {
curTop = parseFloat( curCSSTop ) || 0;
curLeft = parseFloat( curCSSLeft ) || 0;
}
if ( jQuery.isFunction( options ) ) {
2017-05-13 13:25:33 +02:00
// Use jQuery.extend here to allow modification of coordinates argument (gh-1848)
options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
2014-05-12 20:08:19 +02:00
}
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 );
} else {
curElem.css( props );
}
}
};
2017-05-13 13:25:33 +02:00
jQuery.fn.extend( {
2014-05-12 20:08:19 +02:00
offset: function( options ) {
2017-05-13 13:25:33 +02:00
// Preserve chaining for setter
2014-05-12 20:08:19 +02:00
if ( arguments.length ) {
return options === undefined ?
this :
2017-05-13 13:25:33 +02:00
this.each( function( i ) {
2014-05-12 20:08:19 +02:00
jQuery.offset.setOffset( this, options, i );
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
var doc, docElem, rect, win,
elem = this[ 0 ];
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
if ( !elem ) {
2014-05-12 20:08:19 +02:00
return;
}
2017-05-13 13:25:33 +02:00
// Return zeros for disconnected and hidden (display: none) elements (gh-2310)
// Support: IE <=11 only
// Running getBoundingClientRect on a
// disconnected node in IE throws an error
if ( !elem.getClientRects().length ) {
return { top: 0, left: 0 };
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
rect = elem.getBoundingClientRect();
doc = elem.ownerDocument;
docElem = doc.documentElement;
win = doc.defaultView;
2014-05-12 20:08:19 +02:00
return {
2017-05-13 13:25:33 +02:00
top: rect.top + win.pageYOffset - docElem.clientTop,
left: rect.left + win.pageXOffset - docElem.clientLeft
2014-05-12 20:08:19 +02:00
};
},
position: function() {
if ( !this[ 0 ] ) {
return;
}
var offsetParent, offset,
elem = this[ 0 ],
parentOffset = { top: 0, left: 0 };
2017-05-13 13:25:33 +02:00
// Fixed elements are offset from window (parentOffset = {top:0, left: 0},
// because it is its only offset parent
2014-05-12 20:08:19 +02:00
if ( jQuery.css( elem, "position" ) === "fixed" ) {
2017-05-13 13:25:33 +02:00
2016-05-16 13:33:49 +02:00
// Assume getBoundingClientRect is there when computed position is fixed
2014-05-12 20:08:19 +02:00
offset = elem.getBoundingClientRect();
} else {
2017-05-13 13:25:33 +02:00
2014-05-12 20:08:19 +02:00
// Get *real* offsetParent
offsetParent = this.offsetParent();
// Get correct offsets
offset = this.offset();
2017-05-13 13:25:33 +02:00
if ( !nodeName( offsetParent[ 0 ], "html" ) ) {
2014-05-12 20:08:19 +02:00
parentOffset = offsetParent.offset();
}
// Add offsetParent borders
2017-05-13 13:25:33 +02:00
parentOffset = {
top: parentOffset.top + jQuery.css( offsetParent[ 0 ], "borderTopWidth", true ),
left: parentOffset.left + jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true )
};
2014-05-12 20:08:19 +02:00
}
// Subtract parent offsets and element margins
return {
top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
};
},
2017-05-13 13:25:33 +02:00
// 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
2014-05-12 20:08:19 +02:00
offsetParent: function() {
2017-05-13 13:25:33 +02:00
return this.map( function() {
var offsetParent = this.offsetParent;
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
2014-05-12 20:08:19 +02:00
offsetParent = offsetParent.offsetParent;
}
2017-05-13 13:25:33 +02:00
return offsetParent || documentElement;
} );
2014-05-12 20:08:19 +02:00
}
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
// Create scrollLeft and scrollTop methods
jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
var top = "pageYOffset" === prop;
jQuery.fn[ method ] = function( val ) {
return access( this, function( elem, method, val ) {
2017-05-13 13:25:33 +02:00
// Coalesce documents and windows
var win;
if ( jQuery.isWindow( elem ) ) {
win = elem;
} else if ( elem.nodeType === 9 ) {
win = elem.defaultView;
}
2014-05-12 20:08:19 +02:00
if ( val === undefined ) {
return win ? win[ prop ] : elem[ method ];
}
if ( win ) {
win.scrollTo(
2017-05-13 13:25:33 +02:00
!top ? val : win.pageXOffset,
top ? val : win.pageYOffset
2014-05-12 20:08:19 +02:00
);
} else {
elem[ method ] = val;
}
2017-05-13 13:25:33 +02:00
}, method, val, arguments.length );
2014-05-12 20:08:19 +02:00
};
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
2017-05-13 13:25:33 +02:00
// Support: Safari <=7 - 9.1, Chrome <=37 - 49
2014-05-12 20:08:19 +02:00
// Add the top/left cssHooks using jQuery.fn.position
// Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
2017-05-13 13:25:33 +02:00
// Blink bug: https://bugs.chromium.org/p/chromium/issues/detail?id=589347
2016-05-16 13:33:49 +02:00
// 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
2014-05-12 20:08:19 +02:00
jQuery.each( [ "top", "left" ], function( i, prop ) {
jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
function( elem, computed ) {
if ( computed ) {
computed = curCSS( elem, prop );
2017-05-13 13:25:33 +02:00
2016-05-16 13:33:49 +02:00
// If curCSS returns percentage, fallback to offset
2014-05-12 20:08:19 +02:00
return rnumnonpx.test( computed ) ?
jQuery( elem ).position()[ prop ] + "px" :
computed;
}
}
);
2017-05-13 13:25:33 +02:00
} );
2014-05-12 20:08:19 +02:00
return jQuery;
2017-05-13 13:25:33 +02:00
} );