Closing nav menu with Escape key
-
Hi,
as I’m trying to improve accessibility of my website, I would like to be able to close
the “mobile” nav menu by pressing the Escape key.
I have searched other threads for this feature but I have only found a snippet for closing the menu when clicking outside:(function ($) { const $menu = $('.hfe-nav-menu'); $(document).mouseup(e => { if (!$menu.is(e.target) // if the target of the click isn't the container... && $menu.has(e.target).length === 0) // ... nor a descendant of the container { if($('.hfe-nav-menu__toggle').hasClass('hfe-active-menu') && $('.hfe-nav-menu nav').hasClass('menu-is-active')){ $('.hfe-nav-menu__toggle').trigger('click'); } if($('.hfe-has-submenu-container').hasClass('sub-menu-active') && $('.hfe-has-submenu-container').parent().find('ul.sub-menu').hasClass('sub-menu-open')){ $('.hfe-menu-toggle').trigger('click'); } } }); })(jQuery); document.querySelector(".hfe-nav-menu__toggle").addEventListener("click", function(){ document.querySelector("body").classList.toggle("mobile-menu-open"); });I need the same thing but also when pressing the Escape key, can you help me out?
Many thanks.
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘Closing nav menu with Escape key’ is closed to new replies.