Reputation: 592
I got a script for Bootstrap sidebar toggle menu. It works fine for my needs.
Right now the menu is toggling using Toggle Menu button, that is fine.But I need to close the menu if I click, out side of it.
Please check this Fiddle
and help me out. Thanks.
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
Upvotes: 2
Views: 18522
Reputation: 14702
Add a window event to check where you are clicking,
Here is a snnipet :
$("#menu-toggle").click(function(e) {
e.preventDefault();
$("#wrapper").toggleClass("toggled");
});
$(window).click(function(e) {
e.preventDefault();
if(e.target.id =="menu-toggle") return;
if($(e.target).closest('#sidebar-wrapper').length==0){
// small device toogled class added auto so remove it
if($("#wrapper").width() < 768 )
$("#wrapper").removeClass("toggled");
else
$("#wrapper").addClass("toggled");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://blackrockdigital.github.io/startbootstrap-simple-sidebar/js/bootstrap.min.js"></script>
<link href="https://blackrockdigital.github.io/startbootstrap-simple-sidebar/css/simple-sidebar.css" rel="stylesheet"/>
<link href="https://blackrockdigital.github.io/startbootstrap-simple-sidebar/css/bootstrap.min.css" rel="stylesheet"/>
<div id="wrapper">
<!-- Sidebar -->
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li class="sidebar-brand">
<a href="#">
Start Bootstrap
</a>
</li>
<li>
<a href="#">Dashboard</a>
</li>
<li>
<a href="#">Shortcuts</a>
</li>
<li>
<a href="#">Overview</a>
</li>
<li>
<a href="#">Events</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Services</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h1>Simple Sidebar</h1>
<p>This template has a responsive menu toggling system. The menu will appear collapsed on smaller screens, and will appear non-collapsed on larger screens. When toggled using the button below, the menu will appear/disappear. On small screens, the page content will be pushed off canvas.</p>
<p>Make sure to keep all page content within the <code>#page-content-wrapper</code>.</p>
<a href="#menu-toggle" class="btn btn-default" id="menu-toggle">Toggle Menu</a>
</div>
</div>
</div>
</div>
<!-- /#page-content-wrapper -->
</div>
Upvotes: 1
Reputation: 3065
To get this you have to play with jquery functions like .hasClass
, .trigger
, .is
etc. What i did is, i checked the classes on click and then according to them i wrote this code. Please check
See demo
In detail : First i check the class which is toggling on click of a href
so i made a condition that if #wrapper
has toggled class then we will trigger the click event on #menu-toggle
but also we have to careful that click should be on body but not the a href of side bar menu that's why i have added another condition with !tag. Hope you will understand and if not then let me know.
Upvotes: 4