Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaelwhyte/651c634bff1591e3d90b9f7ab416e79e to your computer and use it in GitHub Desktop.
Save michaelwhyte/651c634bff1591e3d90b9f7ab416e79e to your computer and use it in GitHub Desktop.
Show mobile menu and prevent transition on browser re-size
// Responsive Menu - Dropdown
const body = document.body;
const menu = document.getElementById('menu');
const nav = document.getElementById('main-navigation');
// Below code for preventing nav from animating on
// browser re-size modified from code found at
// this stackoverflow question and answer:
//
// https://goo.gl/6s3pAZ
menu.addEventListener('click', openMenu);
function openMenu(){
body.classList.toggle('show');
nav.classList.add('activated');
}
// Media Query Event Listener
// - This is used to remove the "activated"
// class from the navigation when the user
// resizes the browser
// Create a media query list using
// matchMedia
const mql = window.matchMedia('(min-width: 560px)');
// Add a Media Query Listener to the
// above media query list
mql.addListener(removeTransition);
// Function to remove the transition
// from the navigation when the user
// resizes the browser to desktop
// sizes. In this case when the
// screen width becomes wider then
// 560px
function removeTransition(e){
// e -> is the event object
// e.matches -> stores a true false
// value depending if the media query
// list set above matches or not
if(e.matches){
body.classList.remove('show');
nav.classList.remove('activated');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment