简体   繁体   English

单击菜单项时关闭画布导航

[英]Close off-canvas nav when menu item clicked

http://darrenbachan.com/playground/diamond-hand-car-wash/index.html http://darrenbachan.com/playground/diamond-hand-car-wash/index.html

A few things I'm trying to accomplish here: 我要在这里完成的几件事:

  1. When you click anywhere except the menu item it closes the nav 当您单击除菜单项以外的任何位置时,它将关闭导航
  2. When you click on a menu item it closes the nav and animates to an ID 当您单击菜单项时,它将关闭导航并设置动画到ID

The off-canvas nav appears only in it's mobile view. 画布导航仅出现在其移动视图中。 If you view the site on your desktop the nav does animate you to a specific ID, so I have some js there that I'm guessing could be used for the off-canvas one. 如果您在桌面上查看该站点,则导航会为您设置一个特定的ID,因此我在那发现有一些js可用于非画布js。 I've read a few articles saying to use data-toggle but I couldn't get that to work. 我读过几篇文章说要使用数据切换,但无法正常工作。

So I figured I would chime in on this and offer a couple of solutions. 因此,我想我会对此加以介绍,并提供一些解决方案。 If you are just trying to close the menu that you have in place currently when someone clicks on a link in your menu you could very simply either just use a click function to click menu button that you have there like so: 如果当有人单击菜单中的链接时,如果您只是想关闭当前已存在的菜单,则可以很简单地使用单击功能来单击菜单按钮,就像这样:

$('.navbar-collapse li a').on('click', function(){
  $('input.checkbox-toggle').click();
});

Or you could probably just remove the checked from the checkbox like so: 或者,您可以像这样从复选框中删除选中的内容:

$('.navbar-collapse li a').on('click', function(){
  $('input.checkbox-toggle').prop('checked', false);
});

You may want to add an if statement so it only happens at mobile screen sizes so something like the following: 您可能需要添加一个if语句,以便它仅在移动屏幕尺寸下发生,如下所示:

$('.navbar-collapse li a').on('click', function(){
  if($(window).width() <= 767){
    $('input.checkbox-toggle').prop('checked', false);
  }
});

Or vice versa with the click function toggling the click above as well. 反之亦然,点击功能也可以切换上方的点击。

And then you could use your scrolling script that you already have in place and everything should work as you want it to. 然后,您可以使用已经存在的滚动脚本,一切都应按您希望的方式运行。

Or 要么

I see that on your site you are already using bootstrap. 我发现您的网站上已经在使用引导程序。 Bootstrap has a plugin built in that is called scrollspy that handles one page designs allowing you to scroll to sections and things of that nature. Bootstrap内置有一个称为scrollspy的插件,该插件可处理一页设计,使您可以滚动到具有这种性质的部分和内容。 Using this instead of the script that you have in place may be a little more minimal and will serve you better because it will handle replacing active classes in your navbar as well. 使用此脚本代替您已有的脚本可能会稍微少一些,并会为您提供更好的服务,因为它还将处理替换导航栏中的活动类。 It may be much better for what you are trying to achieve with your site. 对于您要通过网站实现的目标可能要好得多。

Also At the bottom I have placed a link to a jsfiddle demo that uses scrollspy along with an off canvas menu that is essentially the same thing that you are using in your site above except you can just use one menu for both large and mobile screens instead of placing two menus on your site. 同样在底部,我放置了一个指向jsfiddle演示的链接,该演示使用scrollspy以及一个画布上的菜单,该菜单与您在上面的站点中使用的基本相同,不同之处在于您只能在大屏幕和移动屏幕上使用一个菜单在您的网站上放置两个菜单。 Look over this fiddle demo it has scrollspy in place and like I said instead of using two menus it just restyles the off canvas menu at mobile screens and uses jquery to toggle a class to the body of .menu-open when the menu button is pressed. 看一下这个小提琴演示,它具有滚动显示的位置,就像我说的,而不是使用两个菜单,它只是在移动屏幕上重新设置了画布上的菜单样式,并在按下菜单按钮时使用jquery将类切换到.menu-open的正文。 。

I wasn't quite sure if you wanted the same kind of overlay style of menu so I made it similar to the site that you have linked to above but if you want it to be a different type of off canvas style then you can just change the css to toggle it from the left or right or wherever. 我不太确定您是否想要相同类型的菜单叠加样式,因此我将其设置为与您上面链接的网站类似,但是如果您希望它是一种不同类型的非画布样式,则只需更改css可从左侧或右侧或任何位置切换它。

Here is a link to the fiddle JSFiddle Demo 这是小提琴JSFiddle演示的链接

Anyway Hope this all helps and if you have any questions feel free to comment below. 无论如何希望这对您有所帮助,如果您有任何疑问,请在下面发表评论。

PS I noticed when looking at your page source that you have .container-fluid wrapping .containers throughout your page. PS:在查看页面源时,我注意到整个页面中都有.container-fluid包装的.containers。 This is not really necessary at all and is more than likely causing the horizontal scrollbar at the bottom. 这一点根本没有必要,而且很可能导致底部的水平滚动条。 I'm not for sure but I just figured I would point this out to you. 我不确定,但我只是想向您指出这一点。

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

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