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