Skip to content
Advertisement

How to play animation only when the element is on screen

I’m working in React, and I need to know when an element is on screen to play a fade animation, and make it appear on screen. Because the animation always plays when the page is loaded, but if you have to scroll to see the element, then you’ll never see the animation 🙁

JavaScript

In my CSS file I have:

JavaScript

What can I do here to only play it when the Grid “AnimationContainer” or when the img “Animation1-1″/”Animation1-2” are visible?

Advertisement

Answer

Use Intersection Observer to detect when the element is visible and only set the animation property once it is. This is pretty easy to do using react-intersection-observer:

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