offset.js (6828B)
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" // contains 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 // Set position first, in-case top/left are set even on static elem 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 // Need to be able to calculate position if either 38 // top or left is auto and position is either absolute or fixed 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 // Use jQuery.extend here to allow modification of coordinates argument (gh-1848) 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 // offset() relates an element's border box to the document origin 74 offset: function( options ) { 75 76 // Preserve chaining for setter 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 // Return zeros for disconnected and hidden (display: none) elements (gh-2310) 93 // Support: IE <=11 only 94 // Running getBoundingClientRect on a 95 // disconnected node in IE throws an error 96 if ( !elem.getClientRects().length ) { 97 return { top: 0, left: 0 }; 98 } 99 100 // Get document-relative position by adding viewport scroll to viewport-relative gBCR 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 // position() relates an element's margin box to its offset parent's padding box 110 // This corresponds to the behavior of CSS absolute positioning 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 // position:fixed elements are offset from the viewport, which itself always has zero offset 121 if ( jQuery.css( elem, "position" ) === "fixed" ) { 122 123 // Assume position:fixed implies availability of getBoundingClientRect 124 offset = elem.getBoundingClientRect(); 125 126 } else { 127 offset = this.offset(); 128 129 // Account for the *real* offset parent, which can be the document or its root element 130 // when a statically positioned element is identified 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 // Incorporate borders into its offset, since they are outside its content origin 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 // Subtract parent offsets and element margins 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 // This method will return documentElement in the following cases: 156 // 1) For the element inside the iframe without offsetParent, this method will return 157 // documentElement of the parent window 158 // 2) For the hidden or detached element 159 // 3) For body or html element, i.e. in case of the html node - it will return itself 160 // 161 // but those exceptions were never presented as a real life use-cases 162 // and might be considered as more preferable results. 163 // 164 // This logic, however, is not guaranteed and can change at any point in the future 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 // Create scrollLeft and scrollTop methods 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 // Coalesce documents and windows 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 // Support: Safari <=7 - 9.1, Chrome <=37 - 49 211 // Add the top/left cssHooks using jQuery.fn.position 212 // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084 213 // Blink bug: https://bugs.chromium.org/p/chromium/issues/detail?id=589347 214 // getComputedStyle returns percent when specified for top/left/bottom/right; 215 // rather than make the css module depend on the offset module, just check for it here 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 // If curCSS returns percentage, fallback to offset 223 return rnumnonpx.test( computed ) ? 224 jQuery( elem ).position()[ prop ] + "px" : 225 computed; 226 } 227 } 228 ); 229 } ); 230 231 return jQuery; 232 } );