$(document).ready(function () { $('.menuLinksDiv>div').hover(function () { const $this = $(this); clearTimeout($this.data('hoverTimeout')); $this.find('.dropdown').stop(true, false).slideDown(200); $this.addClass('open'); }, function () { const $this = $(this); const timeout = setTimeout(function () { $this.find('.dropdown').stop(true, false).slideUp(200); $this.removeClass('open'); }, 200); // Small delay to prevent flickering $this.data('hoverTimeout', timeout); }); $('.sidebar>div').on('click', function (e) { if ($(e.target).closest('.dropdown').length) { return; } if (!$(this).hasClass('open')) { $('.sidebar>div.open').removeClass('open').find('.dropdown').slideUp(); $(this).addClass('open'); $(this).find('.dropdown').slideDown(); } else { $(this).find('.dropdown').slideUp(); $(this).removeClass('open'); } }); $(document).on('click', function (e) { // If sidebar is not active, do nothing if ($(e.target).closest('.burgerMenuDiv').length) { $('.burgerMenuDiv').toggleClass('active'); $('.sidebar').toggleClass('active'); return; } if (!$('.sidebar').hasClass('active')) { return; } // If click is inside the sidebar or burger menu, do nothing if ( $(e.target).closest('.sidebar').length || $(e.target).closest('.burgerMenuDiv').length ) { return; } // Otherwise, close the sidebar $('.sidebar').removeClass('active'); $('.burgerMenuDiv').removeClass('active'); }); });