I have a CSS3 animation that I want to use both in normal and reverse order. I want to trigger the animation by toggling two classes. Basically is an animation that makes an element grow, and I want to use it to shrink the element too.
Basically it is:
- add expanded class: execute animation
- Remove expanded class, add collapsed class: execute animation in reverse
The first part works correctly, however if I try to use the same animation for both classes the animation stops working, nothing happens.
div {
max-height: 1em;
width: 50px;
background-color: red;
color: white;
text-align: center;
border-radius: 30px;
padding: 1em 0;
}
.expanded {
animation: expand 2s;
background-color: blue;
}
.collapsed {
background-color: red;
//animation: expand 2s reverse; // Uncomment and all animations stop working
}
@keyframes expand {
0% {
max-height: 1em;
}
50% {
max-height: 1em;
}
100% {
max-height: 200px;
}
}
Here is a simple jsfiddle that demonstrates this:
https://jsfiddle.net/danielo515/jxLcoocs/6/
EDIT:
As @LGSon have suggested using a transition property will be simpler for this case example, but I need to use an animation because I am running several animations and I need to be able to delay some parts of them.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…