Tween.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. define([
  2. "../core",
  3. "../css"
  4. ], function( jQuery ) {
  5. function Tween( elem, options, prop, end, easing ) {
  6. return new Tween.prototype.init( elem, options, prop, end, easing );
  7. }
  8. jQuery.Tween = Tween;
  9. Tween.prototype = {
  10. constructor: Tween,
  11. init: function( elem, options, prop, end, easing, unit ) {
  12. this.elem = elem;
  13. this.prop = prop;
  14. this.easing = easing || "swing";
  15. this.options = options;
  16. this.start = this.now = this.cur();
  17. this.end = end;
  18. this.unit = unit || ( jQuery.cssNumber[ prop ] ? "" : "px" );
  19. },
  20. cur: function() {
  21. var hooks = Tween.propHooks[ this.prop ];
  22. return hooks && hooks.get ?
  23. hooks.get( this ) :
  24. Tween.propHooks._default.get( this );
  25. },
  26. run: function( percent ) {
  27. var eased,
  28. hooks = Tween.propHooks[ this.prop ];
  29. if ( this.options.duration ) {
  30. this.pos = eased = jQuery.easing[ this.easing ](
  31. percent, this.options.duration * percent, 0, 1, this.options.duration
  32. );
  33. } else {
  34. this.pos = eased = percent;
  35. }
  36. this.now = ( this.end - this.start ) * eased + this.start;
  37. if ( this.options.step ) {
  38. this.options.step.call( this.elem, this.now, this );
  39. }
  40. if ( hooks && hooks.set ) {
  41. hooks.set( this );
  42. } else {
  43. Tween.propHooks._default.set( this );
  44. }
  45. return this;
  46. }
  47. };
  48. Tween.prototype.init.prototype = Tween.prototype;
  49. Tween.propHooks = {
  50. _default: {
  51. get: function( tween ) {
  52. var result;
  53. if ( tween.elem[ tween.prop ] != null &&
  54. (!tween.elem.style || tween.elem.style[ tween.prop ] == null) ) {
  55. return tween.elem[ tween.prop ];
  56. }
  57. // passing an empty string as a 3rd parameter to .css will automatically
  58. // attempt a parseFloat and fallback to a string if the parse fails
  59. // so, simple values such as "10px" are parsed to Float.
  60. // complex values such as "rotate(1rad)" are returned as is.
  61. result = jQuery.css( tween.elem, tween.prop, "" );
  62. // Empty strings, null, undefined and "auto" are converted to 0.
  63. return !result || result === "auto" ? 0 : result;
  64. },
  65. set: function( tween ) {
  66. // use step hook for back compat - use cssHook if its there - use .style if its
  67. // available and use plain properties where available
  68. if ( jQuery.fx.step[ tween.prop ] ) {
  69. jQuery.fx.step[ tween.prop ]( tween );
  70. } else if ( tween.elem.style && ( tween.elem.style[ jQuery.cssProps[ tween.prop ] ] != null || jQuery.cssHooks[ tween.prop ] ) ) {
  71. jQuery.style( tween.elem, tween.prop, tween.now + tween.unit );
  72. } else {
  73. tween.elem[ tween.prop ] = tween.now;
  74. }
  75. }
  76. }
  77. };
  78. // Support: IE9
  79. // Panic based approach to setting things on disconnected nodes
  80. Tween.propHooks.scrollTop = Tween.propHooks.scrollLeft = {
  81. set: function( tween ) {
  82. if ( tween.elem.nodeType && tween.elem.parentNode ) {
  83. tween.elem[ tween.prop ] = tween.now;
  84. }
  85. }
  86. };
  87. jQuery.easing = {
  88. linear: function( p ) {
  89. return p;
  90. },
  91. swing: function( p ) {
  92. return 0.5 - Math.cos( p * Math.PI ) / 2;
  93. }
  94. };
  95. jQuery.fx = Tween.prototype.init;
  96. // Back Compat <1.8 extension point
  97. jQuery.fx.step = {};
  98. });