Close off-canvas sidebar on link click in Jasny Bootstrap

1.3k views Asked by At

I'm developing a site http://maypoprestaurant.com/location

On mobile ( < 768px ), after collpasing the off-canvas navigation via the hamburger icon, I need the off-canvas navigation to close up once one of its links is clicked.

Any help would be GREATLY appreciated as I'm pulling my hair out over here. :x

// Navigation Code

// Navigation Code    
<div id="navA" class="navmenu navmenu-default navmenu-fixed-left offcanvas-xs">
  <a class="navmenu-brand visible-sm visible-md visible-lg" href="#"><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/maypop-logo.png" class="logoSidebar"></a>
  <ul class="nav navmenu-nav">
    <li><a data-toggle="offcanvas" data-target=".navmenu" href="/home2" class="<?php if (is_page('home2')) echo 'active'; ?>">
    <i class="fa fa-home" aria-hidden="true"></i> Home</a></li>

    <li><a data-toggle="offcanvas" data-target=".navmenu" href="/menus" class="<?php if (is_page('menus')) echo 'active'; ?>">
    <i class="fa fa-plus" aria-hidden="true"></i> Menus</a></li>

    <li><a data-toggle="offcanvas" data-target=".navmenu" href="/location" class="<?php if (is_page('location')) echo 'active'; ?>">
    <i class="fa fa-map-marker" aria-hidden="true"></i> Location</a></li>

    <li><a data-toggle="offcanvas" data-target=".navmenu" href="/press" class="<?php if (is_page('press')) echo 'active'; ?>">
    <i class="fa fa-newspaper-o" aria-hidden="true"></i> Press</a></li>

    <li><a data-toggle="offcanvas" data-target=".navmenu" href="/events" class="<?php if (is_page('events')) echo 'active'; ?>">
    <i class="fa fa-calendar-o" aria-hidden="true"></i> Events</a></li> 
  </ul>
</div>

I've added this Javascript in the header:

<script type="text/javascript">
jQuery("#navA .navmenu-nav a").click(function(){
jQuery('.navmenu').offcanvas('hide');
});
</script>

** UPDATE / EDIT**

I have successfully gotten the mobile sidebar to hide on click with this code:

<script type="text/javascript">
jQuery(".navmenu .nav li a").on("click", function (){
jQuery("#navA").removeClass("in canvas-slid");
});
</script>

The issue now is that the hamburger icon doesn't know that I've done this, so it take 2 taps to open the menu instead of just one. I need to somehow tell that event to reset.

Any thoughts?

1

There are 1 answers

1
Rafik Tighilt On

I am not sure that this is the most efficient way, but you can try this :

<script type="text/javascript">
jQuery("#navA .navmenu-nav a").click(function(e){
e.preventDefault();
jQuery('.navmenu').offcanvas('hide');
$(location).attr('href', $(this).attr(href));
});
</script>