jquery.scrollTo.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*!
  2. * jQuery.scrollTo
  3. * Copyright (c) 2007-2015 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
  4. * Licensed under MIT
  5. * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
  6. * @projectDescription Lightweight, cross-browser and highly customizable animated scrolling with jQuery
  7. * @author Ariel Flesler
  8. * @version 2.1.2
  9. */
  10. ;(function(factory) {
  11. 'use strict';
  12. if (typeof define === 'function' && define.amd) {
  13. // AMD
  14. define( ['jquery'], factory );
  15. } else if (typeof module !== 'undefined' && module.exports) {
  16. // CommonJS
  17. module.exports = factory( require( 'jquery' ) );
  18. } else {
  19. // Global
  20. factory( jQuery );
  21. }
  22. })(function($) {
  23. 'use strict';
  24. var $scrollTo = $.scrollTo = function(target, duration, settings) {
  25. return $( window ).scrollTo( target, duration, settings );
  26. };
  27. $scrollTo.defaults = {
  28. axis:'xy',
  29. duration: 0,
  30. limit:true
  31. };
  32. function isWin(elem) {
  33. return ! elem.nodeName ||
  34. $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) !== -1;
  35. }
  36. $.fn.scrollTo = function(target, duration, settings) {
  37. if (typeof duration === 'object') {
  38. settings = duration;
  39. duration = 0;
  40. }
  41. if (typeof settings === 'function') {
  42. settings = { onAfter:settings };
  43. }
  44. if (target === 'max') {
  45. target = 9e9;
  46. }
  47. settings = $.extend( {}, $scrollTo.defaults, settings );
  48. // Speed is still recognized for backwards compatibility
  49. duration = duration || settings.duration;
  50. // Make sure the settings are given right
  51. var queue = settings.queue && settings.axis.length > 1;
  52. if (queue) {
  53. // Let's keep the overall duration
  54. duration /= 2;
  55. }
  56. settings.offset = both( settings.offset );
  57. settings.over = both( settings.over );
  58. return this.each(function() {
  59. // Null target yields nothing, just like jQuery does
  60. if (target === null) { return; }
  61. var win = isWin( this ),
  62. elem = win ? this.contentWindow || window : this,
  63. $elem = $( elem ),
  64. targ = target,
  65. attr = {},
  66. toff;
  67. switch (typeof targ) {
  68. // A number will pass the regex
  69. case 'number':
  70. case 'string':
  71. if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test( targ )) {
  72. targ = both( targ );
  73. // We are done
  74. break;
  75. }
  76. // Relative/Absolute selector
  77. targ = win ? $( targ ) : $( targ, elem );
  78. /* falls through */
  79. case 'object':
  80. if (targ.length === 0) { return; }
  81. // DOMElement / jQuery
  82. if (targ.is || targ.style) {
  83. // Get the real position of the target
  84. toff = (targ = $( targ )).offset();
  85. }
  86. }
  87. var offset = $.isFunction( settings.offset ) && settings.offset( elem, targ ) || settings.offset;
  88. $.each(settings.axis.split( '' ), function(i, axis) {
  89. var Pos = axis === 'x' ? 'Left' : 'Top',
  90. pos = Pos.toLowerCase(),
  91. key = 'scroll' + Pos,
  92. prev = $elem[key](),
  93. max = $scrollTo.max( elem, axis );
  94. if (toff) {// jQuery / DOMElement
  95. attr[key] = toff[pos] + (win ? 0 : prev - $elem.offset()[pos]);
  96. // If it's a dom element, reduce the margin
  97. if (settings.margin) {
  98. attr[key] -= parseInt( targ.css( 'margin' + Pos ), 10 ) || 0;
  99. attr[key] -= parseInt( targ.css( 'border' + Pos + 'Width' ), 10 ) || 0;
  100. }
  101. attr[key] += offset[pos] || 0;
  102. if (settings.over[pos]) {
  103. // Scroll to a fraction of its width/height
  104. attr[key] += targ[axis === 'x'?'width':'height']() * settings.over[pos];
  105. }
  106. } else {
  107. var val = targ[pos];
  108. // Handle percentage values
  109. attr[key] = val.slice && val.slice( -1 ) === '%' ?
  110. parseFloat( val ) / 100 * max
  111. : val;
  112. }
  113. // Number or 'number'
  114. if (settings.limit && /^\d+$/.test( attr[key] )) {
  115. // Check the limits
  116. attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );
  117. }
  118. // Don't waste time animating, if there's no need.
  119. if ( ! i && settings.axis.length > 1) {
  120. if (prev === attr[key]) {
  121. // No animation needed
  122. attr = {};
  123. } else if (queue) {
  124. // Intermediate animation
  125. animate( settings.onAfterFirst );
  126. // Don't animate this axis again in the next iteration.
  127. attr = {};
  128. }
  129. }
  130. });
  131. animate( settings.onAfter );
  132. function animate(callback) {
  133. var opts = $.extend({}, settings, {
  134. // The queue setting conflicts with animate()
  135. // Force it to always be true
  136. queue: true,
  137. duration: duration,
  138. complete: callback && function() {
  139. callback.call( elem, targ, settings );
  140. }
  141. });
  142. $elem.animate( attr, opts );
  143. }
  144. });
  145. };
  146. // Max scrolling position, works on quirks mode
  147. // It only fails (not too badly) on IE, quirks mode.
  148. $scrollTo.max = function(elem, axis) {
  149. var Dim = axis === 'x' ? 'Width' : 'Height',
  150. scroll = 'scroll' + Dim;
  151. if ( ! isWin( elem )) {
  152. return elem[scroll] - $( elem )[Dim.toLowerCase()](); }
  153. var size = 'client' + Dim,
  154. doc = elem.ownerDocument || elem.document,
  155. html = doc.documentElement,
  156. body = doc.body;
  157. return Math.max( html[scroll], body[scroll] ) - Math.min( html[size], body[size] );
  158. };
  159. function both(val) {
  160. return $.isFunction( val ) || $.isPlainObject( val ) ? val : { top:val, left:val };
  161. }
  162. // Add special hooks so that window scroll properties can be animated
  163. $.Tween.propHooks.scrollLeft = $.Tween.propHooks.scrollTop = {
  164. get: function(t) {
  165. return $( t.elem )[t.prop]();
  166. },
  167. set: function(t) {
  168. var curr = this.get( t );
  169. // If interrupt is true and user scrolled, stop animating
  170. if (t.options.interrupt && t._last && t._last !== curr) {
  171. return $( t.elem ).stop();
  172. }
  173. var next = Math.round( t.now );
  174. // Don't waste CPU
  175. // Browsers don't render floating point scroll
  176. if (curr !== next) {
  177. $( t.elem )[t.prop](next);
  178. t._last = this.get( t );
  179. }
  180. }
  181. };
  182. // AMD requirement
  183. return $scrollTo;
  184. });