I have a React app that creates elements that exist for 5 seconds and destroys them. Each of those elements have the same CSS animation assigned:
animation: move-up 5s linear;
The idea is that the element moves from the bottom to the top of the screen in 5 seconds. The app receives messages on when to create one of these elements. It will always destroy them 5 seconds after creation.
This works fine when I have the tab open. However, if I was on another tab, the CSS animation would not play until I tab back in. This is very apparent because the element would be destroyed before it flies to the top of the screen. It gets destroyed correctly after 5 seconds, but the CSS animation does not play immediately and only starts after I tab back in.
Is there any way to kickstart the CSS animation even while on a different tab?
2
Answers
I think you should try to create your animation in javascript like :
It may solve your problem
You need to make sure that your animated element is rendered and not set to
display: none
. You can instead set it tovisibility: hidden
and it should still animate behind the scenes.For example, in the snippet below if you click "add" three times rapidly, you’ll get three animations – one always visible, one with
display: none
, and one withvisibility: hidden
. The two that aren’t always visible will become visible after two seconds. You’ll see that thevisibility: hidden
one will be partially to the top when it shows up, but thedisplay: none
one will start at the bottom (and then be removed about halfway up).