offset.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 top or left is auto and position is either absolute or fixed
  36. if ( calculatePosition ) {
  37. curPosition = curElem.position();
  38. curTop = curPosition.top;
  39. curLeft = curPosition.left;
  40. } else {
  41. curTop = parseFloat( curCSSTop ) || 0;
  42. curLeft = parseFloat( curCSSLeft ) || 0;
  43. }
  44. if ( jQuery.isFunction( options ) ) {
  45. options = options.call( elem, i, curOffset );
  46. }
  47. if ( options.top != null ) {
  48. props.top = ( options.top - curOffset.top ) + curTop;
  49. }
  50. if ( options.left != null ) {
  51. props.left = ( options.left - curOffset.left ) + curLeft;
  52. }
  53. if ( "using" in options ) {
  54. options.using.call( elem, props );
  55. } else {
  56. curElem.css( props );
  57. }
  58. }
  59. };
  60. jQuery.fn.extend({
  61. offset: function( options ) {
  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 docElem, win,
  70. elem = this[ 0 ],
  71. box = { top: 0, left: 0 },
  72. doc = elem && elem.ownerDocument;
  73. if ( !doc ) {
  74. return;
  75. }
  76. docElem = doc.documentElement;
  77. // Make sure it's not a disconnected DOM node
  78. if ( !jQuery.contains( docElem, elem ) ) {
  79. return box;
  80. }
  81. // If we don't have gBCR, just use 0,0 rather than error
  82. // BlackBerry 5, iOS 3 (original iPhone)
  83. if ( typeof elem.getBoundingClientRect !== strundefined ) {
  84. box = elem.getBoundingClientRect();
  85. }
  86. win = getWindow( doc );
  87. return {
  88. top: box.top + win.pageYOffset - docElem.clientTop,
  89. left: box.left + win.pageXOffset - docElem.clientLeft
  90. };
  91. },
  92. position: function() {
  93. if ( !this[ 0 ] ) {
  94. return;
  95. }
  96. var offsetParent, offset,
  97. elem = this[ 0 ],
  98. parentOffset = { top: 0, left: 0 };
  99. // Fixed elements are offset from window (parentOffset = {top:0, left: 0}, because it is its only offset parent
  100. if ( jQuery.css( elem, "position" ) === "fixed" ) {
  101. // We assume that getBoundingClientRect is available when computed position is fixed
  102. offset = elem.getBoundingClientRect();
  103. } else {
  104. // Get *real* offsetParent
  105. offsetParent = this.offsetParent();
  106. // Get correct offsets
  107. offset = this.offset();
  108. if ( !jQuery.nodeName( offsetParent[ 0 ], "html" ) ) {
  109. parentOffset = offsetParent.offset();
  110. }
  111. // Add offsetParent borders
  112. parentOffset.top += jQuery.css( offsetParent[ 0 ], "borderTopWidth", true );
  113. parentOffset.left += jQuery.css( offsetParent[ 0 ], "borderLeftWidth", true );
  114. }
  115. // Subtract parent offsets and element margins
  116. return {
  117. top: offset.top - parentOffset.top - jQuery.css( elem, "marginTop", true ),
  118. left: offset.left - parentOffset.left - jQuery.css( elem, "marginLeft", true )
  119. };
  120. },
  121. offsetParent: function() {
  122. return this.map(function() {
  123. var offsetParent = this.offsetParent || docElem;
  124. while ( offsetParent && ( !jQuery.nodeName( offsetParent, "html" ) && jQuery.css( offsetParent, "position" ) === "static" ) ) {
  125. offsetParent = offsetParent.offsetParent;
  126. }
  127. return offsetParent || docElem;
  128. });
  129. }
  130. });
  131. // Create scrollLeft and scrollTop methods
  132. jQuery.each( { scrollLeft: "pageXOffset", scrollTop: "pageYOffset" }, function( method, prop ) {
  133. var top = "pageYOffset" === prop;
  134. jQuery.fn[ method ] = function( val ) {
  135. return access( this, function( elem, method, val ) {
  136. var win = getWindow( elem );
  137. if ( val === undefined ) {
  138. return win ? win[ prop ] : elem[ method ];
  139. }
  140. if ( win ) {
  141. win.scrollTo(
  142. !top ? val : window.pageXOffset,
  143. top ? val : window.pageYOffset
  144. );
  145. } else {
  146. elem[ method ] = val;
  147. }
  148. }, method, val, arguments.length, null );
  149. };
  150. });
  151. // Add the top/left cssHooks using jQuery.fn.position
  152. // Webkit bug: https://bugs.webkit.org/show_bug.cgi?id=29084
  153. // getComputedStyle returns percent when specified for top/left/bottom/right
  154. // rather than make the css module depend on the offset module, we just check for it here
  155. jQuery.each( [ "top", "left" ], function( i, prop ) {
  156. jQuery.cssHooks[ prop ] = addGetHookIf( support.pixelPosition,
  157. function( elem, computed ) {
  158. if ( computed ) {
  159. computed = curCSS( elem, prop );
  160. // if curCSS returns percentage, fallback to offset
  161. return rnumnonpx.test( computed ) ?
  162. jQuery( elem ).position()[ prop ] + "px" :
  163. computed;
  164. }
  165. }
  166. );
  167. });
  168. return jQuery;
  169. });