Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Last active February 19, 2018 04:41
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 leocaseiro/baee6f5fb0e9be4e40267b8b01693226 to your computer and use it in GitHub Desktop.
Save leocaseiro/baee6f5fb0e9be4e40267b8b01693226 to your computer and use it in GitHub Desktop.
slide-toggle: es6 vanilla javascript with SASS
<button class="js-slide-toggle__btn">toggle</button>
<div class="js-slide-toggle__content">
Nunc nec neque. In turpis. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Aenean tellus metus, bibendum sed, posuere ac, mattis non, nunc. In auctor lobortis lacus.
Etiam sit amet orci eget eros faucibus tincidunt. Proin faucibus arcu quis ante. Sed aliquam ultrices mauris. Aenean viverra rhoncus pede. Donec interdum, metus et hendrerit aliquet, dolor diam sagittis ligula, eget egestas libero turpis vel mi.
</div>
// Reference: http://jsfiddle.net/alistairjcbrown/wJTgA/
window.addEventListener('DOMContentLoaded', () => {
const selector = 'js-slide-toggle';
const buttonEl = document.querySelector(`.${selector}__btn`);
const contentSelector = `${selector}__content`;
if (buttonEl) {
buttonEl.addEventListener('click', () => {
document.querySelector(`.${contentSelector}`).classList.toggle(`${contentSelector}--expanded`);
});
}
});
.js-slide-toggle {
&__content {
max-height: 0;
overflow: hidden;
transition: max-height .5s ease-in-out;
&--expanded {
max-height: 10em;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment