Created
April 7, 2018 12:50
-
-
Save zarcode/cf44a0c43afadf3c0c9151b6d372cc6b to your computer and use it in GitHub Desktop.
chaining CSS transitions with Promise
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
function transitionEndPromise(element) { | |
return new Promise(resolve => { | |
element.addEventListener('transitionend', function f() { | |
element.removeEventListener('transitionend', f); | |
resolve(); | |
}); | |
}); | |
} | |
function animateOut(oldView) { | |
oldView.style.transition = `opacity 2s linear`; | |
oldView.style.opacity = '0'; | |
return transitionEndPromise(oldView); | |
} | |
function animateIn(newView) { | |
newView.style.transition = `opacity 2s linear`; | |
newView.style.opacity = '1'; | |
return transitionEndPromise(newView); | |
} | |
const oldView = document.querySelector('.old-view'); | |
const newView = document.querySelector('.new-view'); | |
const viewAnimation = animateOut(oldView) | |
.then(() => { | |
console.log("done with transition"); | |
animateIn(newView) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment