Tuesday, April 30, 2024
HomeTechnologySoftwareHow to Control CSS Animations with JavaScript

How to Control CSS Animations with JavaScript

Date:

Related stories

2 Times Anger Is Acceptable in Marriage and 3 Times It’s Not

I’m guessing you don’t need a dictionary definition...

What Are Vampire Facial and PRP Treatments—Expert Tips

Vampire facials have earned a reputation for producing...

The Benefits Of Meditation In The Classroom –

contributed by Beth Rush, Managing Edition at Body...

Is NAR A Net-Positive? It Depends If You Ask An Agent Or A Broker: Intel

These increasingly uncertain attitudes among agents in March...
spot_imgspot_img

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!

  • Interview with a Pornhub Web Developer
  • Regular Expressions for the Rest of Us

    Sooner or later you’ll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In…



Source link

Latest stories

spot_img