Skip to content

Instantly share code, notes, and snippets.

@nishanbajracharya
Created December 4, 2018 04:43
Show Gist options
  • Save nishanbajracharya/3b14dc2fa5518bcda021c0eb9d76a835 to your computer and use it in GitHub Desktop.
Save nishanbajracharya/3b14dc2fa5518bcda021c0eb9d76a835 to your computer and use it in GitHub Desktop.
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