Last active
February 19, 2018 04:41
-
-
Save leocaseiro/baee6f5fb0e9be4e40267b8b01693226 to your computer and use it in GitHub Desktop.
slide-toggle: es6 vanilla javascript with SASS
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
<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> |
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
// 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`); | |
}); | |
} | |
}); |
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
.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