easing.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. if (typeof $.easing !== 'undefined') {
  21. $.easing['jswing'] = $.easing['swing'];
  22. }
  23. var pow = Math.pow,
  24. sqrt = Math.sqrt,
  25. sin = Math.sin,
  26. cos = Math.cos,
  27. PI = Math.PI,
  28. c1 = 1.70158,
  29. c2 = c1 * 1.525,
  30. c3 = c1 + 1,
  31. c4 = ( 2 * PI ) / 3,
  32. c5 = ( 2 * PI ) / 4.5;
  33. // x is the fraction of animation progress, in the range 0..1
  34. function bounceOut(x) {
  35. var n1 = 7.5625,
  36. d1 = 2.75;
  37. if ( x < 1/d1 ) {
  38. return n1*x*x;
  39. } else if ( x < 2/d1 ) {
  40. return n1*(x-=(1.5/d1))*x + .75;
  41. } else if ( x < 2.5/d1 ) {
  42. return n1*(x-=(2.25/d1))*x + .9375;
  43. } else {
  44. return n1*(x-=(2.625/d1))*x + .984375;
  45. }
  46. }
  47. $.extend( $.easing,
  48. {
  49. def: 'easeOutQuad',
  50. swing: function (x) {
  51. return $.easing[$.easing.def](x);
  52. },
  53. easeInQuad: function (x) {
  54. return x * x;
  55. },
  56. easeOutQuad: function (x) {
  57. return 1 - ( 1 - x ) * ( 1 - x );
  58. },
  59. easeInOutQuad: function (x) {
  60. return x < 0.5 ?
  61. 2 * x * x :
  62. 1 - pow( -2 * x + 2, 2 ) / 2;
  63. },
  64. easeInCubic: function (x) {
  65. return x * x * x;
  66. },
  67. easeOutCubic: function (x) {
  68. return 1 - pow( 1 - x, 3 );
  69. },
  70. easeInOutCubic: function (x) {
  71. return x < 0.5 ?
  72. 4 * x * x * x :
  73. 1 - pow( -2 * x + 2, 3 ) / 2;
  74. },
  75. easeInQuart: function (x) {
  76. return x * x * x * x;
  77. },
  78. easeOutQuart: function (x) {
  79. return 1 - pow( 1 - x, 4 );
  80. },
  81. easeInOutQuart: function (x) {
  82. return x < 0.5 ?
  83. 8 * x * x * x * x :
  84. 1 - pow( -2 * x + 2, 4 ) / 2;
  85. },
  86. easeInQuint: function (x) {
  87. return x * x * x * x * x;
  88. },
  89. easeOutQuint: function (x) {
  90. return 1 - pow( 1 - x, 5 );
  91. },
  92. easeInOutQuint: function (x) {
  93. return x < 0.5 ?
  94. 16 * x * x * x * x * x :
  95. 1 - pow( -2 * x + 2, 5 ) / 2;
  96. },
  97. easeInSine: function (x) {
  98. return 1 - cos( x * PI/2 );
  99. },
  100. easeOutSine: function (x) {
  101. return sin( x * PI/2 );
  102. },
  103. easeInOutSine: function (x) {
  104. return -( cos( PI * x ) - 1 ) / 2;
  105. },
  106. easeInExpo: function (x) {
  107. return x === 0 ? 0 : pow( 2, 10 * x - 10 );
  108. },
  109. easeOutExpo: function (x) {
  110. return x === 1 ? 1 : 1 - pow( 2, -10 * x );
  111. },
  112. easeInOutExpo: function (x) {
  113. return x === 0 ? 0 : x === 1 ? 1 : x < 0.5 ?
  114. pow( 2, 20 * x - 10 ) / 2 :
  115. ( 2 - pow( 2, -20 * x + 10 ) ) / 2;
  116. },
  117. easeInCirc: function (x) {
  118. return 1 - sqrt( 1 - pow( x, 2 ) );
  119. },
  120. easeOutCirc: function (x) {
  121. return sqrt( 1 - pow( x - 1, 2 ) );
  122. },
  123. easeInOutCirc: function (x) {
  124. return x < 0.5 ?
  125. ( 1 - sqrt( 1 - pow( 2 * x, 2 ) ) ) / 2 :
  126. ( sqrt( 1 - pow( -2 * x + 2, 2 ) ) + 1 ) / 2;
  127. },
  128. easeInElastic: function (x) {
  129. return x === 0 ? 0 : x === 1 ? 1 :
  130. -pow( 2, 10 * x - 10 ) * sin( ( x * 10 - 10.75 ) * c4 );
  131. },
  132. easeOutElastic: function (x) {
  133. return x === 0 ? 0 : x === 1 ? 1 :
  134. pow( 2, -10 * x ) * sin( ( x * 10 - 0.75 ) * c4 ) + 1;
  135. },
  136. easeInOutElastic: function (x) {
  137. return x === 0 ? 0 : x === 1 ? 1 : x < 0.5 ?
  138. -( pow( 2, 20 * x - 10 ) * sin( ( 20 * x - 11.125 ) * c5 )) / 2 :
  139. pow( 2, -20 * x + 10 ) * sin( ( 20 * x - 11.125 ) * c5 ) / 2 + 1;
  140. },
  141. easeInBack: function (x) {
  142. return c3 * x * x * x - c1 * x * x;
  143. },
  144. easeOutBack: function (x) {
  145. return 1 + c3 * pow( x - 1, 3 ) + c1 * pow( x - 1, 2 );
  146. },
  147. easeInOutBack: function (x) {
  148. return x < 0.5 ?
  149. ( pow( 2 * x, 2 ) * ( ( c2 + 1 ) * 2 * x - c2 ) ) / 2 :
  150. ( pow( 2 * x - 2, 2 ) *( ( c2 + 1 ) * ( x * 2 - 2 ) + c2 ) + 2 ) / 2;
  151. },
  152. easeInBounce: function (x) {
  153. return 1 - bounceOut( 1 - x );
  154. },
  155. easeOutBounce: bounceOut,
  156. easeInOutBounce: function (x) {
  157. return x < 0.5 ?
  158. ( 1 - bounceOut( 1 - 2 * x ) ) / 2 :
  159. ( 1 + bounceOut( 2 * x - 1 ) ) / 2;
  160. }
  161. });
  162. });