Skip to content
Advertisement

Trigger CSS Animations in JavaScript

I don’t know how to use JQuery, so I need a method which could trigger an animation using JavaScript only.

I need to call/trigger CSS Animation when the user scrolls the page.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

The simplest method to trigger CSS animations is by adding or removing a class – how to do this with pure Javascript you can read here:

How do I add a class to a given element?

If you DO use jQuery (which should really be easy to learn in basic usage) you do it simply with addClass / removeClass.

All you have to do then is set a transition to a given element like this:

JavaScript

And then change its state if the element has a class:

JavaScript

Note: This example was with transition. But for animations its the same: Just add the animation on the element which has a class on it.

There is a similar question here: Trigger a CSS keyframe animation via scroll

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement