Created
June 12, 2018 12:47
-
-
Save asvny/b1fa7884dd3424dcb18c1b1018166d45 to your computer and use it in GitHub Desktop.
Animate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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