The animate()
method of the Element
is a shortcut for setting CSS Animation on UI elements in JavaScript.
Element
object that needs to be animated based on its id
.animate
API on the Element
object to achieve animation.There are two different formats:
The Animate API also supports specify the Timing Function individually for each Keyframe segment.
An object that contains one or more properties:
Key | Value Type | Optional | Description | Default Value |
---|---|---|---|---|
duration | Number | optional | The length of time for the animation to run. | 0 |
delay | Number | optional | The length of time to wait before starting the animation. | 0 |
iterations | Number | optional | The number of times the animation should repeat. You can set this to Infinity to make the animation loop indefinitely. |
1 |
direction | String | optional | Whether the animation runs forwards (normal ), backwards (reverse ), switches direction after each iteration (alternate ), or runs backwards and switches direction after each iteration (alternate-reverse ). Defaults to "normal" . |
"normal" |
easing | String | optional | The rate of the animation's change over time. Accepts an timing-function, such as "linear" , "ease-in" , or "cubic-bezier(0.42, 0, 0.58, 1)" . Defaults to "linear". |
"linear" |
fill | String | optional | Dictates whether the animation's effects should be reflected by the element(s) prior to playing ("backwards"), retained after the animation has completed playing ("forwards"), or both. Defaults to "none". | "none" |
name | String | optional | The name of the animation, which can be used to uniquely identify it. This name appears in the animation events parameters and is typically used to determine if a particular animation event is the one you're interested in. | An internal unique ID. |
play-state | String | optional | Animation motion state, which defines whether an animation is running or paused, accepts an animation-play-state | running |
Returns an Animation
object.
The Animation
object has the following methods:
Method Name | Description |
---|---|
Animation.cancel() |
Cancels the animation and triggers the animation cancel event. |
Animation.pause() |
Pauses the animation. |
Animation.play() |
Resumes the animation. |
The events for the animate()
API are the same as the animation events for CSS animations.
getElementById("test").animate
will generate a new Animation
object.getElementById("test").animate
is called multiple times for a node, the last created animation will overwrite the previous animation.Animation
object ends, if you want to restart the same animation, you need to call getElementById("test").animate
again to create a new Animation
object.The Animate API may not take effect, possibly because getElementById
failed to select the node: