Created
December 4, 2018 04:43
-
-
Save nishanbajracharya/3b14dc2fa5518bcda021c0eb9d76a835 to your computer and use it in GitHub Desktop.
This file contains 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 animate = document.getElementById('animate'); | |
const btn = document.getElementById('btn'); | |
let enabled = false; | |
btn.onclick = function() { | |
if (enabled) { | |
enabled = false; | |
transition(animate, { | |
remove: ['opening', 'opened'], | |
transitioning: 'closing', | |
transitioned: 'closed' | |
}); | |
} else { | |
enabled = true; | |
transition(animate, { | |
remove: ['closing', 'closed'], | |
transitioning: 'opening', | |
transitioned: 'opened' | |
}); | |
} | |
} | |
const transition = (el, { | |
remove = [], | |
transitioning = '', | |
transitioned = '' | |
}, duration = 1000) => { | |
let timeout; | |
clearTimeout(timeout); | |
el.classList.remove(...remove, transitioning, transitioned); | |
el.classList.add(transitioning); | |
timeout = setTimeout(() => { | |
el.classList.remove(transitioning); | |
el.classList.add(transitioned); | |
clearTimeout(timeout); | |
}, duration); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment