我有一个关键帧和两个按钮.单击"前进"按钮时,我希望长方体向前移动,单击"后退"按钮时,我希望反转关键帧,使长方体向后移动.
这就是我所try 的:
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>