You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

219 lines
5.9 KiB

  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. /**
  15. * Gets a window from an element
  16. */
  17. function getWindow( elem ) {
  18. return jQuery.isWindow( elem ) ? elem : elem.nodeType === 9 && elem.defaultView;
  19. }
  20. jQuery.offset = {
  21. setOffset: function( elem, options, i ) {
  22. var curPosition, curLeft, curCSSTop, curTop, curOffset, curCSSLeft, calculatePosition,
  23. position = jQuery.css( elem, "position" ),
  24. curElem = jQuery( elem ),
  25. props = {};
  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. curOffset = curElem.offset();
  31. curCSSTop = jQuery.css( elem, "top" );
  32. curCSSLeft = jQuery.css( elem, "left" );
  33. calculatePosition = ( position === "absolute" || position === "fixed" ) &&
  34. ( curCSSTop + curCSSLeft ).indexOf( "auto" ) > -1;
  35. // Need to be able to calculate position if either
  36. // top or left is auto and position is either absolute or fixed
  37. if ( calculatePosition ) {
  38. curPosition = curElem.position();
  39. curTop = curPosition.top;
  40. curLeft = curPosition.left;
  41. } else {
  42. curTop = parseFloat( curCSSTop ) || 0;
  43. curLeft = parseFloat( curCSSLeft ) || 0;
  44. }
  45. if ( jQuery.isFunction( options ) ) {
  46. // Use jQuery.extend here to allow modification of coordinates argument (gh-1848)
  47. options = options.call( elem, i, jQuery.extend( {}, curOffset ) );
  48. }
  49. if ( options.top != null ) {
  50. props.top = ( options.top - curOffset.top ) + curTop;
  51. }
  52. if ( options.left != null ) {
  53. props.left = ( options.left - curOffset.left ) + curLeft;
  54. }
  55. if ( "using" in options ) {
  56. options.using.call( elem, props );
  57. } else {
  58. curElem.css( props );
  59. }
  60. }
  61. };
  62. jQuery.fn.extend( {
  63. offset: function( options ) {
  64. if ( arguments.length ) {
  65. return options === undefined ?
  66. this :
  67. this.each( function( i ) {
  68. jQuery.offset.setOffset( this, options, i );
  69. } );
  70. }
  71. var docElem, win,
  72. elem = this[ 0 ],
  73. box = { top: 0, left: 0 },
  74. doc = elem && elem.ownerDocument;
  75. if ( !doc ) {
  76. return;
  77. }
  78. docElem = doc.documentElement;
  79. // Make sure it's not a disconnected DOM node
  80. if ( !jQuery.contains( docElem, elem ) ) {
  81. return box;
  82. }
  83. box = elem.getBoundingClientRect();
  84. win = getWindow( doc );
  85. return {
  86. top: box.top + win.pageYOffset - docElem.clientTop,
  87. left: box.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 ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
  108. parentOffset = offsetParent.offset();
  109. }
  110. // Add offsetParent borders
  111. parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
  112. parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
  113. }
  114. // Subtract parent offsets and element margins
  115. return {
  116. top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
  117. left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
  118. };
  119. },
  120. // This method will return documentElement in the following cases:
  121. // 1) For the element inside the iframe without offsetParent, this method will return
  122. // documentElement of the parent window
  123. // 2) For the hidden or detached element
  124. // 3) For body or html element, i.e. in case of the html node - it will return itself
  125. //
  126. // but those exceptions were never presented as a real life use-cases
  127. // and might be considered as more preferable results.
  128. //
  129. // This logic, however, is not guaranteed and can change at any point in the future
  130. offsetParent: function() {
  131. return this.map( function() {
  132. var offsetParent = this.offsetParent;
  133. while ( offsetParent && jQuery.css( offsetParent, "position" ) === "static" ) {
  134. offsetParent = offsetParent.offsetParent;
  135. }
  136. return offsetParent || documentElement;
  137. } );
  138. }
  139. } );
  140. // Create scrollLeft and scrollTop methods
  141. jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
  142. var top = "pageYOffset" === prop;
  143. jQuery.fn[ method ] = function( val ) {
  144. return access( this, function( elem, method, val ) {
  145. var win = getWindow( elem );
  146. if ( val === undefined ) {
  147. return win ? win[ prop ] : elem[ method ];
  148. }
  149. if ( win ) {
  150. win.scrollTo(
  151. !top ? val : win.pageXOffset,
  152. top ? val : win.pageYOffset
  153. );
  154. } else {
  155. elem[ method ] = val;
  156. }
  157. }, method, val, arguments.length );
  158. };
  159. } );
  160. // Support: Safari<7-8+, Chrome<37-44+
  161. // Add the top/left cssHooks using jQuery.fn.position
  162. // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
  163. // Blink bug: https://code.google.com/p/chromium/issues/detail?id=229280
  164. // getComputedStyle returns percent when specified for top/left/bottom/right;
  165. // rather than make the css module depend on the offset module, just check for it here
  166. jQuery.each( [ "top", "left" ], function( i, prop ) {
  167. jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
  168. function( elem, computed ) {
  169. if ( computed ) {
  170. computed = curCSS( elem, prop );
  171. // If curCSS returns percentage, fallback to offset
  172. return rnumnonpx.test( computed ) ?
  173. jQuery( elem ).position()[ prop ] + "px" :
  174. computed;
  175. }
  176. }
  177. );
  178. } );
  179. return jQuery;
  180. } );