Tween.js 3.2 KB

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