I am trying to have a animation play when an element is selected, and the reverse animation play when the element is deselected. My CSS looks like this:
@keyframes scale-effect {
from {
transform: scale(1);
}
to {
transform: scale(0.75);
}
}
.card.active {
animation: 1s ease-in-out reverse scale-effect;
}
.card.inactive {
animation: 1s ease-in-out forwards scale-effect;
transform: scale(0.75);
}
This plays the correct animations on page load. However, if I try to change the selected class in javascript:
newActiveObject.classList.remove('inactive');
oldActiveObject.classList.remove('active');
oldActiveObject.classList.add('inactive');
newActiveObject.classList.add('active');
Now, the classes get added properly, and I can see the size changes. However, no animation plays.
Things I have tried:
- Using
setTimeout(..., 0)
, between removing and adding classes does not have any effect. Same for 1. - Using
setTimeout(..., 10)
, works, but very clear ugly fash of post-animation style before animation starts - Putting the animation in
.card
, instead of.inactive
. This seems to just disable all animation even on page load. - Using
getComputedStyle
to force DOM redraw in between removing and adding classes. Has no effect.
Does anyone know how to properly replace a animation after removing a class?