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. // 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.
  67. // Use cssHook if its there.
  68. // Use .style if available and use plain properties where available.
  69. if ( jQuery.fx.step[ tween.prop ] ) {
  70. jQuery.fx.step[ tween.prop ]( tween );
  71. } else if ( tween.elem.style && ( tween.elem.style[ jQuery.cssProps[ tween.prop ] ] != null || jQuery.cssHooks[ tween.prop ] ) ) {
  72. jQuery.style( tween.elem, tween.prop, tween.now + tween.unit );
  73. } else {
  74. tween.elem[ tween.prop ] = tween.now;
  75. }
  76. }
  77. }
  78. };
  79. // Support: IE9
  80. // Panic based approach to setting things on disconnected nodes
  81. Tween.propHooks.scrollTop = Tween.propHooks.scrollLeft = {
  82. set: function( tween ) {
  83. if ( tween.elem.nodeType && tween.elem.parentNode ) {
  84. tween.elem[ tween.prop ] = tween.now;
  85. }
  86. }
  87. };
  88. jQuery.easing = {
  89. linear: function( p ) {
  90. return p;
  91. },
  92. swing: function( p ) {
  93. return 0.5 - Math.cos( p * Math.PI ) / 2;
  94. }
  95. };
  96. jQuery.fx = Tween.prototype.init;
  97. // Back Compat <1.8 extension point
  98. jQuery.fx.step = {};
  99. });