Tuesday, December 27, 2022
HomeWeb DevelopmentLearn how to Management CSS Animations with JavaScript

Learn how to Management CSS Animations with JavaScript


In the case of animations on the net, builders have to measure the animation’s necessities with the correct know-how — CSS or JavaScript. Many animations are manageable with CSS however JavaScript will at all times present extra management. With doc.getAnimations, nevertheless, you should use JavaScript to handle CSS animations!

The doc.getAnimations methodology returns an array of CSSAnimation objects. CSSAnimation supplies a number of details about the animation: playState, timeline, impact, and occasions like onfinish. You may then modify these objects to regulate animations:

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

// Cease all CSS animations on the web page
doc.getAnimations().forEach((animation) => {
  animation.cancel();
});

How might adjusting CSS animations on the fly be helpful to builders? Possibly use the Battery API to cease all animations when the system battery is low. Probably to cease animations when the consumer has scrolled previous the animation itself.

I like the way in which you should use JavaScript to change CSS animations. Builders used to want to decide on between CSS and JavaScript — now we’ve got the instruments to make them work collectively!

  • 7 Essential JavaScript Functions

    I keep in mind the early days of JavaScript the place you wanted a easy operate for almost every little thing as a result of the browser distributors carried out options in another way, and never simply edge options, primary options, like addEventListener and attachEvent.  Instances have modified however there are nonetheless just a few capabilities every developer ought to…

  • Chris Coyier’s Favorite CodePen Demos

    David requested me if I might be up for a visitor put up choosing out a few of my favourite Pens from CodePen. A frightening activity! There are such a lot of! I managed to select just a few although which have blown me away over the previous few months. For those who…


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments