sonata_admin_js_jquery.scrollTo_2.js 5.4 KB

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