简体   繁体   English

在Jasny Bootstrap中单击链接,关闭画布侧边栏

[英]Close off-canvas sidebar on link click in Jasny Bootstrap

I'm developing a site http://maypoprestaurant.com/location 我正在开发一个网站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. 在移动设备(<768px)上,通过汉堡包图标关闭画布导航后,一旦单击其链接之一,我就需要关闭画布导航。

Any help would be GREATLY appreciated as I'm pulling my hair out over here. 当我在这里拉头发时,任何帮助将不胜感激。 :x :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: 我已经在标头中添加了此Javascript:

<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. 现在的问题是,汉堡图标不知道我已经这样做了,因此需要2次点击才能打开菜单,而不仅仅是一次。 I need to somehow tell that event to reset. 我需要以某种方式告诉该事件重置。

Any thoughts? 有什么想法吗?

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM