/uatyper/static/js/jquery.scrollTo-1.4.2.js

https://bitbucket.org/crchemist/uatyper.com · JavaScript · 215 lines · 108 code · 29 blank · 78 comment · 39 complexity · 8fadd26de44aa6a1bff9bfb134f99e8d MD5 · raw file

  1. /**
  2. * jQuery.ScrollTo
  3. * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
  4. * Dual licensed under MIT and GPL.
  5. * Date: 5/25/2009
  6. *
  7. * @projectDescription Easy element scrolling using jQuery.
  8. * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
  9. * Works with jQuery +1.2.6. Tested on FF 2/3, IE 6/7/8, Opera 9.5/6, Safari 3, Chrome 1 on WinXP.
  10. *
  11. * @author Ariel Flesler
  12. * @version 1.4.2
  13. *
  14. * @id jQuery.scrollTo
  15. * @id jQuery.fn.scrollTo
  16. * @param {String, Number, DOMElement, jQuery, Object} target Where to scroll the matched elements.
  17. * The different options for target are:
  18. * - A number position (will be applied to all axes).
  19. * - A string position ('44', '100px', '+=90', etc ) will be applied to all axes
  20. * - A jQuery/DOM element ( logically, child of the element to scroll )
  21. * - A string selector, that will be relative to the element to scroll ( 'li:eq(2)', etc )
  22. * - A hash { top:x, left:y }, x and y can be any kind of number/string like above.
  23. * - A percentage of the container's dimension/s, for example: 50% to go to the middle.
  24. * - The string 'max' for go-to-end.
  25. * @param {Number} duration The OVERALL length of the animation, this argument can be the settings object instead.
  26. * @param {Object,Function} settings Optional set of settings or the onAfter callback.
  27. * @option {String} axis Which axis must be scrolled, use 'x', 'y', 'xy' or 'yx'.
  28. * @option {Number} duration The OVERALL length of the animation.
  29. * @option {String} easing The easing method for the animation.
  30. * @option {Boolean} margin If true, the margin of the target element will be deducted from the final position.
  31. * @option {Object, Number} offset Add/deduct from the end position. One number for both axes or { top:x, left:y }.
  32. * @option {Object, Number} over Add/deduct the height/width multiplied by 'over', can be { top:x, left:y } when using both axes.
  33. * @option {Boolean} queue If true, and both axis are given, the 2nd axis will only be animated after the first one ends.
  34. * @option {Function} onAfter Function to be called after the scrolling ends.
  35. * @option {Function} onAfterFirst If queuing is activated, this function will be called after the first scrolling ends.
  36. * @return {jQuery} Returns the same jQuery object, for chaining.
  37. *
  38. * @desc Scroll to a fixed position
  39. * @example $('div').scrollTo( 340 );
  40. *
  41. * @desc Scroll relatively to the actual position
  42. * @example $('div').scrollTo( '+=340px', { axis:'y' } );
  43. *
  44. * @dec Scroll using a selector (relative to the scrolled element)
  45. * @example $('div').scrollTo( 'p.paragraph:eq(2)', 500, { easing:'swing', queue:true, axis:'xy' } );
  46. *
  47. * @ Scroll to a DOM element (same for jQuery object)
  48. * @example var second_child = document.getElementById('container').firstChild.nextSibling;
  49. * $('#container').scrollTo( second_child, { duration:500, axis:'x', onAfter:function(){
  50. * alert('scrolled!!');
  51. * }});
  52. *
  53. * @desc Scroll on both axes, to different values
  54. * @example $('div').scrollTo( { top: 300, left:'+=200' }, { axis:'xy', offset:-20 } );
  55. */
  56. ;(function( $ ){
  57. var $scrollTo = $.scrollTo = function( target, duration, settings ){
  58. $(window).scrollTo( target, duration, settings );
  59. };
  60. $scrollTo.defaults = {
  61. axis:'xy',
  62. duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
  63. };
  64. // Returns the element that needs to be animated to scroll the window.
  65. // Kept for backwards compatibility (specially for localScroll & serialScroll)
  66. $scrollTo.window = function( scope ){
  67. return $(window)._scrollable();
  68. };
  69. // Hack, hack, hack :)
  70. // Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
  71. $.fn._scrollable = function(){
  72. return this.map(function(){
  73. var elem = this,
  74. isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;
  75. if( !isWin )
  76. return elem;
  77. var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
  78. return $.browser.safari || doc.compatMode == 'BackCompat' ?
  79. doc.body :
  80. doc.documentElement;
  81. });
  82. };
  83. $.fn.scrollTo = function( target, duration, settings ){
  84. if( typeof duration == 'object' ){
  85. settings = duration;
  86. duration = 0;
  87. }
  88. if( typeof settings == 'function' )
  89. settings = { onAfter:settings };
  90. if( target == 'max' )
  91. target = 9e9;
  92. settings = $.extend( {}, $scrollTo.defaults, settings );
  93. // Speed is still recognized for backwards compatibility
  94. duration = duration || settings.speed || settings.duration;
  95. // Make sure the settings are given right
  96. settings.queue = settings.queue && settings.axis.length > 1;
  97. if( settings.queue )
  98. // Let's keep the overall duration
  99. duration /= 2;
  100. settings.offset = both( settings.offset );
  101. settings.over = both( settings.over );
  102. return this._scrollable().each(function(){
  103. var elem = this,
  104. $elem = $(elem),
  105. targ = target, toff, attr = {},
  106. win = $elem.is('html,body');
  107. switch( typeof targ ){
  108. // A number will pass the regex
  109. case 'number':
  110. case 'string':
  111. if( /^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
  112. targ = both( targ );
  113. // We are done
  114. break;
  115. }
  116. // Relative selector, no break!
  117. targ = $(targ,this);
  118. case 'object':
  119. // DOMElement / jQuery
  120. if( targ.is || targ.style )
  121. // Get the real position of the target
  122. toff = (targ = $(targ)).offset();
  123. }
  124. $.each( settings.axis.split(''), function( i, axis ){
  125. var Pos = axis == 'x' ? 'Left' : 'Top',
  126. pos = Pos.toLowerCase(),
  127. key = 'scroll' + Pos,
  128. old = elem[key],
  129. max = $scrollTo.max(elem, axis);
  130. if( toff ){// jQuery / DOMElement
  131. attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );
  132. // If it's a dom element, reduce the margin
  133. if( settings.margin ){
  134. attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
  135. attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
  136. }
  137. attr[key] += settings.offset[pos] || 0;
  138. if( settings.over[pos] )
  139. // Scroll to a fraction of its width/height
  140. attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
  141. }else{
  142. var val = targ[pos];
  143. // Handle percentage values
  144. attr[key] = val.slice && val.slice(-1) == '%' ?
  145. parseFloat(val) / 100 * max
  146. : val;
  147. }
  148. // Number or 'number'
  149. if( /^\d+$/.test(attr[key]) )
  150. // Check the limits
  151. attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );
  152. // Queueing axes
  153. if( !i && settings.queue ){
  154. // Don't waste time animating, if there's no need.
  155. if( old != attr[key] )
  156. // Intermediate animation
  157. animate( settings.onAfterFirst );
  158. // Don't animate this axis again in the next iteration.
  159. delete attr[key];
  160. }
  161. });
  162. animate( settings.onAfter );
  163. function animate( callback ){
  164. $elem.animate( attr, duration, settings.easing, callback && function(){
  165. callback.call(this, target, settings);
  166. });
  167. };
  168. }).end();
  169. };
  170. // Max scrolling position, works on quirks mode
  171. // It only fails (not too badly) on IE, quirks mode.
  172. $scrollTo.max = function( elem, axis ){
  173. var Dim = axis == 'x' ? 'Width' : 'Height',
  174. scroll = 'scroll'+Dim;
  175. if( !$(elem).is('html,body') )
  176. return elem[scroll] - $(elem)[Dim.toLowerCase()]();
  177. var size = 'client' + Dim,
  178. html = elem.ownerDocument.documentElement,
  179. body = elem.ownerDocument.body;
  180. return Math.max( html[scroll], body[scroll] )
  181. - Math.min( html[size] , body[size] );
  182. };
  183. function both( val ){
  184. return typeof val == 'object' ? val : { top:val, left:val };
  185. };
  186. })( jQuery );