JSFiddle - React, Tailwind, and code Playground
by Terry Mun
HTML
<div id="div_one"></div>
<div id="div_two"></div>
CSS
#div_one {
width: 50px;
height: 50px;
border: 10px solid red;
}
#div_two {
width: 50px;
height: 50px;
border: 10px solid blue;
}
@keyframes move {
100% {
transform: translateX(300px);
}
}
@keyframes down {
100% {
transform: translateY(300px);
}
}
JavaScript
// We can declare a generic helper method for one-time animationend listening
let onceAnimationEnd = (el, animation) => {
return new Promise(resolve => {
const onAnimationEndCb = () => {
el.removeEventListener('animationend', onAnimationEndCb);
resolve();
}
el.addEventListener('animationend', onAnimationEndCb)
el.style.animation = animation;
});
}
let move_box_one = async () => {
const el = document.getElementById('div_one');
await onceAnimationEnd(el, 'move 3s forwards');
}
let move_box_two = async () => {
const el = document.getElementById('div_two');
await onceAnimationEnd(el, 'move 3s forwards');
}
let move_boxes = async () => {
await move_box_one();
await move_box_two();
}
move_boxes().then(() => console.log('boxes moved'));