Hide /toggle if clicked outside

前端 未结 2 1134
走了就别回头了
走了就别回头了 2021-01-26 01:00

I am using this template over here ( http://startbootstrap.com/templates/simple-sidebar.html ) for my startup in bootstrap learning. I am finding it pretty good to learn. Howeve

相关标签:
2条回答
  • 2021-01-26 01:58

    Try this instead:

    $("#menu-toggle").click(function(e) {
        e.preventDefault();
        $("#wrapper").toggleClass("active");
    });
    
    $("#page-content-wrapper").click(function(e) {
        e.preventDefault();
        if $("#wrapper").hasClass("active"){
          $("#wrapper").toggleClass("active");
        }
    });
    

    This will allow you to listen for the click on the menu-toggle button when the sidebar is hidden and listen for the click on the rest of the content when the sidebar is visible.

    0 讨论(0)
  • 2021-01-26 01:58

    You're going to need to create an absolutely positioned div covering the content area you want to click to hide the sidebar you'll have to specify its width and height via CSS. It will also need its own click event. Think of it as kind of a huge button that covers the content that is on the left side. This example does not cover the case of hiding the div if you navigate to a different section.

    $('#menu-toggle').click(function(e) {
     $('.covers-content').show();
    });
    
    $('.covers-content').onClick(function(e) {
     $('#wrapper').toggleClass("active");
     $('.covers-content').hide();
    });
    
    <div class="covers-content" style="display:none"></div>
    
    0 讨论(0)
提交回复
热议问题