Created
July 16, 2023 22:54
-
-
Save devwax/f766a666e32d9b18fe48d7234788b489 to your computer and use it in GitHub Desktop.
jQuery snippet that toggles an Elementor popup while respecting Esc key and other non-click events like clicking outside the toggle button or menu.
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
jQuery(function ($) { | |
// Initialize toggler state | |
window.mytheme_mobile_menu_toggle_open = false; | |
var popup_id = 7627; | |
var delay = 50; // Delay to avoid Elementor events firing before click event handlers (milliseconds) | |
// Mobile nav button toggle click event | |
$('li.mobile-menu-toggle').on('click', function (e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
if (window.mytheme_mobile_menu_toggle_open) { // Popup is open, close it. | |
elementorProFrontend.modules.popup.closePopup({ id: popup_id }, e); | |
} else { // Popup is closed, open it. | |
elementorProFrontend.modules.popup.showPopup({ id: popup_id }, e); | |
} | |
// Toggle the global state variable after a delay | |
setTimeout(function() { | |
window.mytheme_mobile_menu_toggle_open = !window.mytheme_mobile_menu_toggle_open; | |
}, delay); | |
}); | |
// Elementor popup events | |
$(document).on('elementor/popup/hide', (event, id, instance) => { | |
if (id === popup_id) { | |
window.mytheme_mobile_menu_toggle_open = false; | |
} | |
}); | |
$(document).on('elementor/popup/show', (event, id, instance) => { | |
if (id === popup_id) { | |
window.mytheme_mobile_menu_toggle_open = true; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment