jquery.scrollTo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*!
  2. * jQuery.scrollTo
  3. * Copyright (c) 2007-2014 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 Easy element scrolling using jQuery.
  7. * @author Ariel Flesler
  8. * @version 1.4.14
  9. */
  10. ;(function(define) {
  11. 'use strict';
  12. define(['jquery'], function($) {
  13. var $scrollTo = $.scrollTo = function(target, duration, settings) {
  14. return $(window).scrollTo(target, duration, settings);
  15. };
  16. $scrollTo.defaults = {
  17. axis:'xy',
  18. duration: 0,
  19. limit:true
  20. };
  21. // Returns the element that needs to be animated to scroll the window.
  22. // Kept for backwards compatibility (specially for localScroll & serialScroll)
  23. $scrollTo.window = function() {
  24. return $(window)._scrollable();
  25. };
  26. // Hack, hack, hack :)
  27. // Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
  28. $.fn._scrollable = function() {
  29. return this.map(function() {
  30. var elem = this,
  31. isWin = !elem.nodeName || $.inArray(elem.nodeName.toLowerCase(), ['iframe','#document','html','body']) !== -1;
  32. if (!isWin) {
  33. return elem;
  34. }
  35. var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
  36. return /webkit/i.test(navigator.userAgent) || doc.compatMode === 'BackCompat' ?
  37. doc.body :
  38. doc.documentElement;
  39. });
  40. };
  41. $.fn.scrollTo = function(target, duration, settings) {
  42. if (typeof duration === 'object') {
  43. settings = duration;
  44. duration = 0;
  45. }
  46. if (typeof settings === 'function') {
  47. settings = { onAfter:settings };
  48. }
  49. if (target === 'max') {
  50. target = 9e9;
  51. }
  52. settings = $.extend({}, $scrollTo.defaults, settings);
  53. // Speed is still recognized for backwards compatibility
  54. duration = duration || settings.duration;
  55. // Make sure the settings are given right
  56. settings.queue = settings.queue && settings.axis.length > 1;
  57. if (settings.queue) {
  58. // Let's keep the overall duration
  59. duration /= 2;
  60. }
  61. settings.offset = both(settings.offset);
  62. settings.over = both(settings.over);
  63. return this._scrollable().each(function() {
  64. // Null target yields nothing, just like jQuery does
  65. if (target === null) return;
  66. var elem = this,
  67. $elem = $(elem),
  68. targ = target, toff, attr = {},
  69. win = $elem.is('html,body');
  70. switch (typeof targ) {
  71. // A number will pass the regex
  72. case 'number':
  73. case 'string':
  74. if (/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)) {
  75. targ = both(targ);
  76. // We are done
  77. break;
  78. }
  79. // Relative/Absolute selector, no break!
  80. targ = win ? $(targ) : $(targ, this);
  81. if (!targ.length) return;
  82. /* falls through */
  83. case 'object':
  84. // DOMElement / jQuery
  85. if (targ.is || targ.style) {
  86. // Get the real position of the target
  87. toff = (targ = $(targ)).offset();
  88. }
  89. }
  90. var offset = $.isFunction(settings.offset) && settings.offset(elem, targ) || settings.offset;
  91. $.each(settings.axis.split(''), function(i, axis) {
  92. var Pos = axis === 'x' ? 'Left' : 'Top',
  93. pos = Pos.toLowerCase(),
  94. key = 'scroll' + Pos,
  95. old = elem[key],
  96. max = $scrollTo.max(elem, axis);
  97. if (toff) {// jQuery / DOMElement
  98. attr[key] = toff[pos] + (win ? 0 : old - $elem.offset()[pos]);
  99. // If it's a dom element, reduce the margin
  100. if (settings.margin) {
  101. attr[key] -= parseInt(targ.css('margin'+Pos), 10) || 0;
  102. attr[key] -= parseInt(targ.css('border'+Pos+'Width'), 10) || 0;
  103. }
  104. attr[key] += offset[pos] || 0;
  105. if (settings.over[pos]) {
  106. // Scroll to a fraction of its width/height
  107. attr[key] += targ[axis === 'x'?'width':'height']() * settings.over[pos];
  108. }
  109. } else {
  110. var val = targ[pos];
  111. // Handle percentage values
  112. attr[key] = val.slice && val.slice(-1) === '%' ?
  113. parseFloat(val) / 100 * max
  114. : val;
  115. }
  116. // Number or 'number'
  117. if (settings.limit && /^\d+$/.test(attr[key])) {
  118. // Check the limits
  119. attr[key] = attr[key] <= 0 ? 0 : Math.min(attr[key], max);
  120. }
  121. // Queueing axes
  122. if (!i && settings.queue) {
  123. // Don't waste time animating, if there's no need.
  124. if (old !== attr[key]) {
  125. // Intermediate animation
  126. animate(settings.onAfterFirst);
  127. }
  128. // Don't animate this axis again in the next iteration.
  129. delete attr[key];
  130. }
  131. });
  132. animate(settings.onAfter);
  133. function animate(callback) {
  134. $elem.animate(attr, duration, settings.easing, callback && function() {
  135. callback.call(this, targ, settings);
  136. });
  137. }
  138. }).end();
  139. };
  140. // Max scrolling position, works on quirks mode
  141. // It only fails (not too badly) on IE, quirks mode.
  142. $scrollTo.max = function(elem, axis) {
  143. var Dim = axis === 'x' ? 'Width' : 'Height',
  144. scroll = 'scroll'+Dim;
  145. if (!$(elem).is('html,body'))
  146. return elem[scroll] - $(elem)[Dim.toLowerCase()]();
  147. var size = 'client' + Dim,
  148. html = elem.ownerDocument.documentElement,
  149. body = elem.ownerDocument.body;
  150. return Math.max(html[scroll], body[scroll]) - Math.min(html[size], body[size]);
  151. };
  152. function both(val) {
  153. return $.isFunction(val) || $.isPlainObject(val) ? val : { top:val, left:val };
  154. }
  155. // AMD requirement
  156. return $scrollTo;
  157. });
  158. }(typeof define === 'function' && define.amd ? define : function(deps, factory) {
  159. 'use strict';
  160. if (typeof module !== 'undefined' && module.exports) {
  161. // Node
  162. module.exports = factory(require('jquery'));
  163. } else {
  164. factory(jQuery);
  165. }
  166. }));