| 1 | define( [
|
| 2 | "./core",
|
| 3 | "./core/access",
|
| 4 | "./var/documentElement",
|
| 5 | "./var/isFunction",
|
| 6 | "./css/var/rnumnonpx",
|
| 7 | "./css/curCSS",
|
| 8 | "./css/addGetHookIf",
|
| 9 | "./css/support",
|
| 10 | "./var/isWindow",
|
| 11 | "./core/init",
|
| 12 | "./css",
|
| 13 | "./selector"
|
| 14 | ], function( jQuery, access, documentElement, isFunction, rnumnonpx,
|
| 15 | curCSS, addGetHookIf, support, isWindow ) {
|
| 16 |
|
| 17 | "use strict";
|
| 18 |
|
| 19 | jQuery.offset = {
|
| 20 | setOffset: function( elem, options, i ) {
|
| 21 | var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
|
| 22 | position = jQuery.css( elem, "position" ),
|
| 23 | curElem = jQuery( elem ),
|
| 24 | props = {};
|
| 25 |
|
| 26 |
|
| 27 | if ( position === "static" ) {
|
| 28 | elem.style.position = "relative";
|
| 29 | }
|
| 30 |
|
| 31 | curOffset = curElem.offset();
|
| 32 | curCSSTop = jQuery.css( elem, "top" );
|
| 33 | curCSSLeft = jQuery.css( elem, "left" );
|
| 34 | calculatePosition = ( position === "absolute" || position === "fixed" ) &&
|
| 35 | ( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1;
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 | if ( calculatePosition ) {
|
| 40 | curPosition = curElem.position();
|
| 41 | curTop = curPosition.top;
|
| 42 | curLeft = curPosition.left;
|
| 43 |
|
| 44 | } else {
|
| 45 | curTop = parseFloat( curCSSTop ) || 0;
|
| 46 | curLeft = parseFloat( curCSSLeft ) || 0;
|
| 47 | }
|
| 48 |
|
| 49 | if ( isFunction( options ) ) {
|
| 50 |
|
| 51 |
|
| 52 | options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
|
| 53 | }
|
| 54 |
|
| 55 | if ( options.top != null ) {
|
| 56 | props.top = ( options.top - curOffset.top ) + curTop;
|
| 57 | }
|
| 58 | if ( options.left != null ) {
|
| 59 | props.left = ( options.left - curOffset.left ) + curLeft;
|
| 60 | }
|
| 61 |
|
| 62 | if ( "using" in options ) {
|
| 63 | options.using.call( elem, props );
|
| 64 |
|
| 65 | } else {
|
| 66 | curElem.css( props );
|
| 67 | }
|
| 68 | }
|
| 69 | };
|
| 70 |
|
| 71 | jQuery.fn.extend( {
|
| 72 |
|
| 73 |
|
| 74 | offset: function( options ) {
|
| 75 |
|
| 76 |
|
| 77 | if ( arguments.length ) {
|
| 78 | return options === undefined ?
|
| 79 | this :
|
| 80 | this.each( function( i ) {
|
| 81 | jQuery.offset.setOffset( this, options, i );
|
| 82 | } );
|
| 83 | }
|
| 84 |
|
| 85 | var rect, win,
|
| 86 | elem = this[ 0 ];
|
| 87 |
|
| 88 | if ( !elem ) {
|
| 89 | return;
|
| 90 | }
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 | if ( !elem.getClientRects().length ) {
|
| 97 | return { top: 0, left: 0 };
|
| 98 | }
|
| 99 |
|
| 100 |
|
| 101 | rect = elem.getBoundingClientRect();
|
| 102 | win = elem.ownerDocument.defaultView;
|
| 103 | return {
|
| 104 | top: rect.top + win.pageYOffset,
|
| 105 | left: rect.left + win.pageXOffset
|
| 106 | };
|
| 107 | },
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 | position: function() {
|
| 112 | if ( !this[ 0 ] ) {
|
| 113 | return;
|
| 114 | }
|
| 115 |
|
| 116 | var offsetParent, offset, doc,
|
| 117 | elem = this[ 0 ],
|
| 118 | parentOffset = { top: 0, left: 0 };
|
| 119 |
|
| 120 |
|
| 121 | if ( jQuery.css( elem, "position" ) === "fixed" ) {
|
| 122 |
|
| 123 |
|
| 124 | offset = elem.getBoundingClientRect();
|
| 125 |
|
| 126 | } else {
|
| 127 | offset = this.offset();
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 | doc = elem.ownerDocument;
|
| 132 | offsetParent = elem.offsetParent || doc.documentElement;
|
| 133 | while ( offsetParent &&
|
| 134 | ( offsetParent === doc.body || offsetParent === doc.documentElement ) &&
|
| 135 | jQuery.css( offsetParent, "position" ) === "static" ) {
|
| 136 |
|
| 137 | offsetParent = offsetParent.parentNode;
|
| 138 | }
|
| 139 | if ( offsetParent && offsetParent !== elem && offsetParent.nodeType === 1 ) {
|
| 140 |
|
| 141 |
|
| 142 | parentOffset = jQuery( offsetParent ).offset();
|
| 143 | parentOffset.top += jQuery.css( offsetParent, "borderTopWidth", true );
|
| 144 | parentOffset.left += jQuery.css( offsetParent, "borderLeftWidth", true );
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 |
|
| 149 | return {
|
| 150 | top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
|
| 151 | left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
|
| 152 | };
|
| 153 | },
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 | offsetParent: function() {
|
| 166 | return this.map( function() {
|
| 167 | var offsetParent = this.offsetParent;
|
| 168 |
|
| 169 | while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
|
| 170 | offsetParent = offsetParent.offsetParent;
|
| 171 | }
|
| 172 |
|
| 173 | return offsetParent || documentElement;
|
| 174 | } );
|
| 175 | }
|
| 176 | } );
|
| 177 |
|
| 178 |
|
| 179 | jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
|
| 180 | var top = "pageYOffset" === prop;
|
| 181 |
|
| 182 | jQuery.fn[ method ] = function( val ) {
|
| 183 | return access( this, function( elem, method, val ) {
|
| 184 |
|
| 185 |
|
| 186 | var win;
|
| 187 | if ( isWindow( elem ) ) {
|
| 188 | win = elem;
|
| 189 | } else if ( elem.nodeType === 9 ) {
|
| 190 | win = elem.defaultView;
|
| 191 | }
|
| 192 |
|
| 193 | if ( val === undefined ) {
|
| 194 | return win ? win[ prop ] : elem[ method ];
|
| 195 | }
|
| 196 |
|
| 197 | if ( win ) {
|
| 198 | win.scrollTo(
|
| 199 | !top ? val : win.pageXOffset,
|
| 200 | top ? val : win.pageYOffset
|
| 201 | );
|
| 202 |
|
| 203 | } else {
|
| 204 | elem[ method ] = val;
|
| 205 | }
|
| 206 | }, method, val, arguments.length );
|
| 207 | };
|
| 208 | } );
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 | jQuery.each( [ "top", "left" ], function( _i, prop ) {
|
| 217 | jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
|
| 218 | function( elem, computed ) {
|
| 219 | if ( computed ) {
|
| 220 | computed = curCSS( elem, prop );
|
| 221 |
|
| 222 |
|
| 223 | return rnumnonpx.test( computed ) ?
|
| 224 | jQuery( elem ).position()[ prop ] + "px" :
|
| 225 | computed;
|
| 226 | }
|
| 227 | }
|
| 228 | );
|
| 229 | } );
|
| 230 |
|
| 231 | return jQuery;
|
| 232 | } );
|