So I have a pretty straightforward example here where I apply an animation through JS to grow my inner div. However, upon trying to reverse the animation with JS, I am finding it does not transition; rather, it suddenly snaps back to the original state.
Example:
const grow = document.querySelector('.grow');
const reverse = document.querySelector('.reverse');
const inner = document.querySelector('.inner');
grow.addEventListener('click', function() {
inner.classList.add('grow');
})
//things I've tried
// reverse.addEventListener('click', function() {
// inner.classList.add('reverse');
// })
reverse.addEventListener('click', function() {
inner.style.animationDirection = 'reverse';
})
html, body {
height: 100%;
width: 100%;
margin: 0;
}
.container {
height: 100%;
width: 100%;
background: orange;
position: relative;
}
.inner {
position: absolute;
bottom: 0;
height: 52px;
width: 80%;
background: #fff;
}
.grow {
animation: grow forwards 1s ease-in-out;
}
.reverse {
animation: grow reverse 1s ease-in-out;
}
@keyframes grow {
from {
height: 52px;
}
to {
height: 100%;
}
}
<div class="container">
<div class="inner">
<button class="grow">Grow</button>
<button class="reverse">Reverse</button>
</div>
</div>
Can someone explain to me why this is happening, and how I can revert the transition back smoothly without adding a separate keyframe labeled 'shrink'?