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