Answer the question
In order to leave comments, you need to log in
Add CSS animation via JavaScript?
Hello Toaster!
I'm trying to figure out the problem - how to add CSS animation through JavaScript anywhere in the code? I can paste the CSS animation code by hand:
@keyframes anim {
0% {
background-color: yellow;
}
100% {
background-color: green;
}
}
#anim {
animation: anim 2s ease;
}
document.getElementById("anim").style.animation = "anim 2s ease";
Answer the question
In order to leave comments, you need to log in
To do this, use css classes
Let's say we have a set of animations and classes with them, in animation.css
@keyframes anim {
0% {
background-color: yellow;
}
100% {
background-color: green;
}
}
.main-animation {
animation: anim 2s ease;
}
document.querySelector('.box').className="main-animation";
//если надо убрать анимацию то удаляем класс и всё
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question