Skip to content

Instantly share code, notes, and snippets.

@terion-name
Last active May 26, 2016 12:37
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 terion-name/cebfedfa495e4f8ad900c16aad03b5c7 to your computer and use it in GitHub Desktop.
Save terion-name/cebfedfa495e4f8ad900c16aad03b5c7 to your computer and use it in GitHub Desktop.
slideUp / slideDown on transition
export default class Spoiler {
constructor(container) {
this.container = container;
this.opener = this.container.querySelector('.opener');
this.content = this.container.querySelector('.content');
this.isAnimating = false;
this.opened = false;
this.container.addEventListener(transitionend, ()=>{
this.isAnimating = false;
if (this.opened) {
this.content.style.maxHeight = 'none';
}
});
this.opener.addEventListener('click', (e)=>{
e.preventDefault();
this.opened ? this.close() : this.open()
})
}
open() {
if (this.isAnimating || this.opened) {
return;
}
this.content.style.maxHeight = this.content.scrollHeight + 'px';
this.isAnimating = true;
this.opened = true;
}
close() {
if (this.isAnimating || !this.opened) {
return;
}
this.content.style.maxHeight = this.content.scrollHeight + 'px';
this.isAnimating = true;
this.opened = false;
setTimeout(()=>{
this.content.style.maxHeight = 0;
}, 10);
}
}
.spoiler
.content
max-height 0
overflow hidden
transition max-height 0.5s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment