Hide menu sidebar when clicking outside the bar or the button

前端 未结 5 1618
粉色の甜心
粉色の甜心 2020-12-31 22:29

I am trying to make a menu like the semantic UI but I only achieved to click the menu button and open the menu and vice versa. I use toggle class to show the sidebar but I d

5条回答
  •  攒了一身酷
    2020-12-31 22:59

    Edit your js code to following

    $('#menu-button').click(function(e){
        e.stopPropagation();
        $('#hide-menu').toggleClass('show-menu');
    });
    
    $('#hide-menu').click(function(e){
        e.stopPropagation();
    });
    
    $('body,html').click(function(e){
       $('#hide-menu').removeClass('show-menu');
    });
    

    Hope this will work.

    Here is fiddle. http://jsfiddle.net/ex8ddv5q/1/

提交回复
热议问题