Ei kuvausta

Tween.js 3.2KB

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