Skip to content

Instantly share code, notes, and snippets.

@fauzanmy
Created January 17, 2021 08:41
Show Gist options
  • Save fauzanmy/6e9439be61524bc9e74a3a79703a3c18 to your computer and use it in GitHub Desktop.
Save fauzanmy/6e9439be61524bc9e74a3a79703a3c18 to your computer and use it in GitHub Desktop.
Vanilla javascript toggle sidebar body overlay.
<script>
let burger = document.querySelector('.burger');
let close = document.querySelector('.close');
let sidenav = document.querySelector('#sidenav');
let overlay = document.querySelector('#overlay');
let classOpen = [sidenav, overlay];
burger.addEventListener('click', function(e){
classOpen.forEach(e => e.classList.add('active'));
});
let classCloseClick = [overlay, close];
classCloseClick.forEach(function(el) {
el.addEventListener('click', function(els) {
classOpen.forEach(els => els.classList.remove('active'));
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment