且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当您在容器外部单击时,关闭汉堡菜单

更新时间:2023-02-13 18:31:59

技巧是检查单击的元素是否具有父.responsive-menu.menu-btn.

The trick is to check if the element clicked has a parent .responsive-menu or .menu-btn.

如果两者都不存在且菜单已展开,请切换!

If it has'n any of the two and the menu is expanded, toggle!

更新了小提琴

$(document).on("click", function(e){
  if( 
    $(e.target).closest(".responsive-menu").length == 0 &&
    $(".responsive-menu").hasClass("expand") &&
    $(e.target).closest(".menu-btn").length == 0
  ){
    $('.responsive-menu').toggleClass('expand');
  }
});