How to Control CSS Animations with JavaScript

When it comes to animations on the web, developers need to measure the animation’s requirements with the right technology — CSS or JavaScript. Many animations are manageable with CSS but JavaScript will always provide more control. With document.getAnimations, however, you can use JavaScript to manage CSS animations! The document.getAnimations method returns an array of CSSAnimation […]

The post How to Control CSS Animations with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

When it comes to animations on the web, developers need to measure the animation’s requirements with the right technology — CSS or JavaScript. Many animations are manageable with CSS but JavaScript will always provide more control. With document.getAnimations, however, you can use JavaScript to manage CSS animations!

The document.getAnimations method returns an array of CSSAnimation objects. CSSAnimation provides a host of information about the animation: playState, timeline, effect, and events like onfinish. You can then modify those objects to adjust animations:

// Make all CSS animations on the page twice as fast
document.getAnimations().forEach((animation) => {
  animation.playbackRate *= 2;
});

// Stop all CSS animations on the page
document.getAnimations().forEach((animation) => {
  animation.cancel();
});

How could adjusting CSS animations on the fly be useful to developers? Maybe use the Battery API to stop all animations when the device battery is low. Possibly to stop animations when the user has scrolled past the animation itself.

I love the way you can use JavaScript to modify CSS animations. Developers used to need to choose between CSS and JavaScript — now we have the tools to make them work together!

The post How to Control CSS Animations with JavaScript appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2022-12-27T20:02:45+00:00) How to Control CSS Animations with JavaScript. Retrieved from https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/

MLA
" » How to Control CSS Animations with JavaScript." David Walsh | Sciencx - Tuesday December 27, 2022, https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/
HARVARD
David Walsh | Sciencx Tuesday December 27, 2022 » How to Control CSS Animations with JavaScript., viewed ,<https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/>
VANCOUVER
David Walsh | Sciencx - » How to Control CSS Animations with JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/
CHICAGO
" » How to Control CSS Animations with JavaScript." David Walsh | Sciencx - Accessed . https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/
IEEE
" » How to Control CSS Animations with JavaScript." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/. [Accessed: ]
rf:citation
» How to Control CSS Animations with JavaScript | David Walsh | Sciencx | https://www.scien.cx/2022/12/27/how-to-control-css-animations-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.