offset.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. define([
  2. "./core",
  3. "./var/strundefined",
  4. "./core/access",
  5. "./css/var/rnumnonpx",
  6. "./css/curCSS",
  7. "./css/addGetHookIf",
  8. "./css/support",
  9. "./core/init",
  10. "./css",
  11. "./selector" // contains
  12. ], function( jQuery, strundefined, access, rnumnonpx, curCSS, addGetHookIf, support ) {
  13. var docElem = window.document.documentElement;
  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. options = options.call( elem, i, curOffset );
  47. }
  48. if ( options.top != null ) {
  49. props.top = ( options.top - curOffset.top ) + curTop;
  50. }
  51. if ( options.left != null ) {
  52. props.left = ( options.left - curOffset.left ) + curLeft;
  53. }
  54. if ( "using" in options ) {
  55. options.using.call( elem, props );
  56. } else {
  57. curElem.css( props );
  58. }
  59. }
  60. };
  61. jQuery.fn.extend({
  62. offset: function( options ) {
  63. if ( arguments.length ) {
  64. return options === undefined ?
  65. this :
  66. this.each(function( i ) {
  67. jQuery.offset.setOffset( this, options, i );
  68. });
  69. }
  70. var docElem, win,
  71. elem = this[ 0 ],
  72. box = { top: 0, left: 0 },
  73. doc = elem && elem.ownerDocument;
  74. if ( !doc ) {
  75. return;
  76. }
  77. docElem = doc.documentElement;
  78. // Make sure it's not a disconnected DOM node
  79. if ( !jQuery.contains( docElem, elem ) ) {
  80. return box;
  81. }
  82. // Support: BlackBerry 5, iOS 3 (original iPhone)
  83. // If we don't have gBCR, just use 0,0 rather than error
  84. if ( typeof elem.getBoundingClientRect !== strundefined ) {
  85. box = elem.getBoundingClientRect();
  86. }
  87. win = getWindow( doc );
  88. return {
  89. top: box.top + win.pageYOffset - docElem.clientTop,
  90. left: box.left + win.pageXOffset - docElem.clientLeft
  91. };
  92. },
  93. position: function() {
  94. if ( !this[ 0 ] ) {
  95. return;
  96. }
  97. var offsetParent, offset,
  98. elem = this[ 0 ],
  99. parentOffset = { top: 0, left: 0 };
  100. // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
  101. if ( jQuery.css( elem, "position" ) === "fixed" ) {
  102. // Assume getBoundingClientRect is there when computed position is fixed
  103. offset = elem.getBoundingClientRect();
  104. } else {
  105. // Get *real* offsetParent
  106. offsetParent = this.offsetParent();
  107. // Get correct offsets
  108. offset = this.offset();
  109. if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
  110. parentOffset = offsetParent.offset();
  111. }
  112. // Add offsetParent borders
  113. parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
  114. parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
  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. offsetParent: function() {
  123. return this.map(function() {
  124. var offsetParent = this.offsetParent || docElem;
  125. while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position" ) === "static" ) ) {
  126. offsetParent = offsetParent.offsetParent;
  127. }
  128. return offsetParent || docElem;
  129. });
  130. }
  131. });
  132. // Create scrollLeft and scrollTop methods
  133. jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
  134. var top = "pageYOffset" === prop;
  135. jQuery.fn[ method ] = function( val ) {
  136. return access( this, function( elem, method, val ) {
  137. var win = getWindow( elem );
  138. if ( val === undefined ) {
  139. return win ? win[ prop ] : elem[ method ];
  140. }
  141. if ( win ) {
  142. win.scrollTo(
  143. !top ? val : window.pageXOffset,
  144. top ? val : window.pageYOffset
  145. );
  146. } else {
  147. elem[ method ] = val;
  148. }
  149. }, method, val, arguments.length, null );
  150. };
  151. });
  152. // Support: Safari<7+, Chrome<37+
  153. // Add the top/left cssHooks using jQuery.fn.position
  154. // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
  155. // Blink bug: https://code.google.com/p/chromium/issues/detail?id=229280
  156. // getComputedStyle returns percent when specified for top/left/bottom/right;
  157. // rather than make the css module depend on the offset module, just check for it here
  158. jQuery.each( [ "top", "left" ], function( i, prop ) {
  159. jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
  160. function( elem, computed ) {
  161. if ( computed ) {
  162. computed = curCSS( elem, prop );
  163. // If curCSS returns percentage, fallback to offset
  164. return rnumnonpx.test( computed ) ?
  165. jQuery( elem ).position()[ prop ] + "px" :
  166. computed;
  167. }
  168. }
  169. );
  170. });
  171. return jQuery;
  172. });