Skip to content

Instantly share code, notes, and snippets.

@zarcode
Created April 7, 2018 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarcode/cf44a0c43afadf3c0c9151b6d372cc6b to your computer and use it in GitHub Desktop.
Save zarcode/cf44a0c43afadf3c0c9151b6d372cc6b to your computer and use it in GitHub Desktop.
chaining CSS transitions with Promise
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