jquery.easing.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * jQuery Easing v1.4.1 - http://gsgd.co.uk/sandbox/jquery/easing/
  3. * Open source under the BSD License.
  4. * Copyright © 2008 George McGinley Smith
  5. * All rights reserved.
  6. * https://raw.github.com/gdsmith/jquery-easing/master/LICENSE
  7. */
  8. (function (factory) {
  9. if (typeof define === "function" && define.amd) {
  10. define(['jquery'], function ($) {
  11. return factory($);
  12. });
  13. } else if (typeof module === "object" && typeof module.exports === "object") {
  14. exports = factory(require('jquery'));
  15. } else {
  16. factory(jQuery);
  17. }
  18. })(function($){
  19. // Preserve the original jQuery "swing" easing as "jswing"
  20. $.easing.jswing = $.easing.swing;
  21. var pow = Math.pow,
  22. sqrt = Math.sqrt,
  23. sin = Math.sin,
  24. cos = Math.cos,
  25. PI = Math.PI,
  26. c1 = 1.70158,
  27. c2 = c1 * 1.525,
  28. c3 = c1 + 1,
  29. c4 = ( 2 * PI ) / 3,
  30. c5 = ( 2 * PI ) / 4.5;
  31. // x is the fraction of animation progress, in the range 0..1
  32. function bounceOut(x) {
  33. var n1 = 7.5625,
  34. d1 = 2.75;
  35. if ( x < 1/d1 ) {
  36. return n1*x*x;
  37. } else if ( x < 2/d1 ) {
  38. return n1*(x-=(1.5/d1))*x + 0.75;
  39. } else if ( x < 2.5/d1 ) {
  40. return n1*(x-=(2.25/d1))*x + 0.9375;
  41. } else {
  42. return n1*(x-=(2.625/d1))*x + 0.984375;
  43. }
  44. }
  45. $.extend( $.easing,
  46. {
  47. def: 'easeOutQuad',
  48. swing: function (x) {
  49. return $.easing[$.easing.def](x);
  50. },
  51. easeInQuad: function (x) {
  52. return x * x;
  53. },
  54. easeOutQuad: function (x) {
  55. return 1 - ( 1 - x ) * ( 1 - x );
  56. },
  57. easeInOutQuad: function (x) {
  58. return x < 0.5 ?
  59. 2 * x * x :
  60. 1 - pow( -2 * x + 2, 2 ) / 2;
  61. },
  62. easeInCubic: function (x) {
  63. return x * x * x;
  64. },
  65. easeOutCubic: function (x) {
  66. return 1 - pow( 1 - x, 3 );
  67. },
  68. easeInOutCubic: function (x) {
  69. return x < 0.5 ?
  70. 4 * x * x * x :
  71. 1 - pow( -2 * x + 2, 3 ) / 2;
  72. },
  73. easeInQuart: function (x) {
  74. return x * x * x * x;
  75. },
  76. easeOutQuart: function (x) {
  77. return 1 - pow( 1 - x, 4 );
  78. },
  79. easeInOutQuart: function (x) {
  80. return x < 0.5 ?
  81. 8 * x * x * x * x :
  82. 1 - pow( -2 * x + 2, 4 ) / 2;
  83. },
  84. easeInQuint: function (x) {
  85. return x * x * x * x * x;
  86. },
  87. easeOutQuint: function (x) {
  88. return 1 - pow( 1 - x, 5 );
  89. },
  90. easeInOutQuint: function (x) {
  91. return x < 0.5 ?
  92. 16 * x * x * x * x * x :
  93. 1 - pow( -2 * x + 2, 5 ) / 2;
  94. },
  95. easeInSine: function (x) {
  96. return 1 - cos( x * PI/2 );
  97. },
  98. easeOutSine: function (x) {
  99. return sin( x * PI/2 );
  100. },
  101. easeInOutSine: function (x) {
  102. return -( cos( PI * x ) - 1 ) / 2;
  103. },
  104. easeInExpo: function (x) {
  105. return x === 0 ? 0 : pow( 2, 10 * x - 10 );
  106. },
  107. easeOutExpo: function (x) {
  108. return x === 1 ? 1 : 1 - pow( 2, -10 * x );
  109. },
  110. easeInOutExpo: function (x) {
  111. return x === 0 ? 0 : x === 1 ? 1 : x < 0.5 ?
  112. pow( 2, 20 * x - 10 ) / 2 :
  113. ( 2 - pow( 2, -20 * x + 10 ) ) / 2;
  114. },
  115. easeInCirc: function (x) {
  116. return 1 - sqrt( 1 - pow( x, 2 ) );
  117. },
  118. easeOutCirc: function (x) {
  119. return sqrt( 1 - pow( x - 1, 2 ) );
  120. },
  121. easeInOutCirc: function (x) {
  122. return x < 0.5 ?
  123. ( 1 - sqrt( 1 - pow( 2 * x, 2 ) ) ) / 2 :
  124. ( sqrt( 1 - pow( -2 * x + 2, 2 ) ) + 1 ) / 2;
  125. },
  126. easeInElastic: function (x) {
  127. return x === 0 ? 0 : x === 1 ? 1 :
  128. -pow( 2, 10 * x - 10 ) * sin( ( x * 10 - 10.75 ) * c4 );
  129. },
  130. easeOutElastic: function (x) {
  131. return x === 0 ? 0 : x === 1 ? 1 :
  132. pow( 2, -10 * x ) * sin( ( x * 10 - 0.75 ) * c4 ) + 1;
  133. },
  134. easeInOutElastic: function (x) {
  135. return x === 0 ? 0 : x === 1 ? 1 : x < 0.5 ?
  136. -( pow( 2, 20 * x - 10 ) * sin( ( 20 * x - 11.125 ) * c5 )) / 2 :
  137. pow( 2, -20 * x + 10 ) * sin( ( 20 * x - 11.125 ) * c5 ) / 2 + 1;
  138. },
  139. easeInBack: function (x) {
  140. return c3 * x * x * x - c1 * x * x;
  141. },
  142. easeOutBack: function (x) {
  143. return 1 + c3 * pow( x - 1, 3 ) + c1 * pow( x - 1, 2 );
  144. },
  145. easeInOutBack: function (x) {
  146. return x < 0.5 ?
  147. ( pow( 2 * x, 2 ) * ( ( c2 + 1 ) * 2 * x - c2 ) ) / 2 :
  148. ( pow( 2 * x - 2, 2 ) *( ( c2 + 1 ) * ( x * 2 - 2 ) + c2 ) + 2 ) / 2;
  149. },
  150. easeInBounce: function (x) {
  151. return 1 - bounceOut( 1 - x );
  152. },
  153. easeOutBounce: bounceOut,
  154. easeInOutBounce: function (x) {
  155. return x < 0.5 ?
  156. ( 1 - bounceOut( 1 - 2 * x ) ) / 2 :
  157. ( 1 + bounceOut( 2 * x - 1 ) ) / 2;
  158. }
  159. });
  160. });