Skip to content

Instantly share code, notes, and snippets.

@asvny
Created June 12, 2018 12:47
Show Gist options
  • Save asvny/b1fa7884dd3424dcb18c1b1018166d45 to your computer and use it in GitHub Desktop.
Save asvny/b1fa7884dd3424dcb18c1b1018166d45 to your computer and use it in GitHub Desktop.
Animate
const rafPromise = _ => new Promise(r => requestAnimationFrame(r));
const transEndPromise = ele => new Promise(r => {
ele.addEventListener('transitionend', function f() {
ele.removeEventListener('transitionend', f);
r();
});
});
const animate = (ele, styles) => {
Object.assign(ele.style, styles);
return transEndPromise(ele)
.then(_ => rafPromise());
}
/// EXAMPLE
const element = document.querySelector('.box');
async function main() {
await rafPromise();
await animate(element, {transform: 'translateX(400px)', backgroundColor: 'blue'})
await animate(element, {transform: 'translateY(200px) scale(0.5)', backgroundColor: 'green'});
await animate(element, {transform: 'translateX(0px)', backgroundColor: 'red'})
main();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment