I have a keyframe and 2 buttons. When clicking on the forward button, I want my box to move forwards and when clicking on the back button, I want to reverse my keyframe, so the box should move back.
This is what I have tried:
const box = document.querySelector('.box');
document.getElementById('right').addEventListener('click', () => {
box.classList.add('active');
});
document.getElementById('left').addEventListener('click', () => {
box.style.animationDirection = 'reverse';
// Also tried it with: box.classList.remove('active');
});
.box {
width: 100px;
height: 100px;
background: blue;
position: relative;
}
.active {
animation: move 1s forwards;
}
@keyframes move {
from {
left: 0;
}
to {
left: 20px;
}
}
<button id="left"><</button>
<button id="right">></button>
<div class="box"></div>