offset.js 6.1 KB

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