offset.js 6.2 KB

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