You can set the property of animation
to "none"
and use setTimeout
that will empty the property of animation
to empty ""
and then it will inherit the CSS property again. >
Example:
variavel = document.getElementById("butt");
variavel.onclick = function efeito() {
var e = document.getElementById("div");
e.style.animation = "none";
setTimeout(function() {
e.style.animation = "";
}, 100);
}
div {
width: 100px;
height: 100px;
background-color: red;
animation: nomeAnimacao 4s linear;
}
@keyframes nomeAnimacao {
0% {background-color: red;}
25% {background-color: yellow;}
50% {background-color: blue;}
100% {background-color: green;}
}
<div id="div"></div>
<br>
<button id="butt" type="button">Reiniciar</button>
Edit
When you hit "none"
in an animation, it is automatically canceled, returning to its initial state. By emptying the style, it takes over the CSS style and restarts. See the image that the style accepts none
:
Forotherstyles,suchaswidth
,thebehaviorissimilarexceptthatwidth
doesnotacceptnone
.Here'sanexample:
variavel = document.getElementById("dvd");
variavel.onclick = function efeito() {
var e = document.getElementById("div");
e.style.width = "100px";
setTimeout(function() {
e.style.width = "";
}, 2000);
}
div{
width: 300px;
height: 50px;
background: red;
}
<div id="div">Clique no botão e aguarde 2s</div>
<input type="button" value="ok" id="dvd">