Reputation: 409
Is there a way to restart a CSS animation without cloning elements, reflowing DOM, waiting (setTimeout/onAnimationEnd)?
Upvotes: 5
Views: 7392
Reputation: 1624
The simplest and most reliable way to "restart" CSS animations I've found is this answer: https://stackoverflow.com/a/72525340/3705191
animation1
and animation2
)animation
property@keyframes popup1 {
0% {
transform: scale(0.95);
}
100% {
transform: scale(1);
}
}
@keyframes popup2 {
0% {
transform: scale(0.95);
}
100% {
transform: scale(1);
}
}
.animated-element {
animation: popup1 200ms ease;
}
// when wanting to restart:
const el = document.$('.animated-element');
// animationName only returns the name
if( el.style.animationName == 'popup1' ){
el.style.animation = 'popup2 200ms ease';
} else {
el.style.animation = 'popup1 200ms ease';
}
Upvotes: 1
Reputation: 409
EDIT: No jQuery or check needed.
I basically just restart the animation at the next painted frame.
This method doesn't clone any element, reflows the document, setting any timeout, or waiting for completion of the animation. It is 100% flexible and requires no jQuery.
I haven't seen anything similar (not even at css-tricks) so I wanted to share my idea with you and hear your thoughts.
document.querySelector('.box').onclick = function(){
requestAnimation( this );
};
var requestAnimation = function( obj ){
obj.style.animation = 'none';
window.requestAnimationFrame(function(){
obj.style.animation = 'myAnimation 1s';
});
}
html{ background: #212121; }
.box{
width: 150px;
background: white;
cursor: pointer;
text-align: center;
margin: auto;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 3px black;
}
span{
float: right;
color: grey;
}
@keyframes myAnimation{
from{ background: grey; transform: scale(1.2); }
to{ background: white; transform: scale(1); }
}
<div class="box">I can animate forever!</div>
<span>By Luca Rossi</span>
Upvotes: 11