Last active
April 1, 2021 20:31
-
-
Save generatepress/589b9e91bca7205883b0454f15a4dcf9 to your computer and use it in GitHub Desktop.
Initiate custom menu hamburger: https://generatepress.com/forums/topic/animated-hamburger-icon
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
<script> | |
var hamburger = document.querySelector(".hamburger"), | |
menuToggle = document.querySelector( '.menu-toggle' ), | |
menuItems = document.querySelectorAll( 'nav ul a' ), | |
closeElements = document.querySelectorAll( '.slideout-overlay, .slider-exit a' ); | |
menuToggle.addEventListener("click", function() { | |
hamburger.classList.toggle("is-active"); | |
} ); | |
for ( var i = 0; i < closeElements.length; i++ ) { | |
closeElements[i].addEventListener( 'click', function( e ) { | |
hamburger.classList.toggle("is-active"); | |
} ); | |
}; | |
for ( var i = 0; i < menuItems.length; i++ ) { | |
menuItems[i].addEventListener( 'click', function( e ) { | |
var closest_nav = this.closest( 'nav' ); | |
if ( closest_nav.classList.contains( 'toggled' ) || htmlEl.classList.contains( 'slide-opened' ) ) { | |
var url = this.getAttribute( 'href' ); | |
var hash = url.split('#')[1]; | |
// Open the sub-menu if the link has no destination | |
if ( hash ) { | |
e.preventDefault(); | |
hamburger.classList.toggle("is-active"); | |
} | |
} | |
}, false ); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment