javascript - jquery how to hide-sidebar and remove class when clicked somewhere on the page -


ihave jquery code works show offcanvas sidebar , makes page opacity lower toggling class acive main-wrapper class. code

$(document).ready(function () {     $('.nav-icon').on('click', function(){        $('.offcanvas').toggleclass('is-open');        $('.main-wrapper').toggleclass('active');     }); // end of on click      }); // end of ready 

what want how remove 2 toggled classes when clicked on page except .nav-icon class. , html code.

<div id="wrapper" class="main-wrapper">     <div class="off-canvas-wrapper">         <div id="offcanvasleft" class="offcanvas" aria-hidden="true">             <ul class="nav menus flex-column">                 <li class="nav-item">                     <a class="nav-link active" href="#">artists</a>                  </li>                  <li class="nav-item">                     <a class="nav-link" href="#">link2</a>                 </li>                  <li class="nav-item">                     <a class="nav-link" href="#">link3</a>                 </li>             </ul>         </div>     </div>     <!-- off-canvas-wrapper -->       <nav class="navbar">          <i class="fa fa-bars fa-2x nav-icon" aria-hidden="true"></i>      </nav>      <div id="main-section" class="container"> 

you can examine event parameter .on() handler this:

$(document).ready(function () {      $('.nav-icon').on('click', function(){        $('.offcanvas').toggleclass('is-open');        $('.main-wrapper').toggleclass('active');     });      $('html').on('click', function(event){         if(!$(event.target).hasclass('nav-icon'))         {             $('.offcanvas').removeclass('is-open');             $('.main-wrapper').removeclass('active');                  }     });   }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -