简体   繁体   English

单击导航项时关闭导航

[英]Close navigation when navigation item is clicked

My goal is for my hamburger menu to close when an item is clicked inside of it. 我的目标是当在其中单击某个项目时关闭我的汉堡菜单。 As of right now, the menu only uses html and css. 截至目前,菜单仅使用html和css。

The difference between this nav bar and others is that mine is created from a input checkbox html element, what i need is for my checkbox to uncheck when a link is clicked inside of the hamburger. 该导航栏和其他导航栏之间的区别是,我的导航栏是通过输入复选框html元素创建的,当需要在汉堡包中单击链接时,我需要取消复选框的选择。 This should close the entire menu just like it would if i clicked on the hamburger. 这应该关闭整个菜单,就像我单击汉堡包一样。 Also, could you explain what and why the javascript does what it does, i don't have much experience with javascript, thanks. 另外,您能解释一下什么以及为什么javascript会做什么吗,我对javascript没有太多经验,谢谢。 :) :)

I also made the checkbox visible just so that we can have a better understanding of whats going on. 我还使复选框可见,以便我们可以更好地了解正在发生的事情。

My CSS: 我的CSS:

 /* navigation menu */ .nav { position: absolute; top: 0; left: 0; width: 100%; height: 70px; line-height: 70px; text-align: right; z-index: 10000; background-color: #ffffff; border-bottom: 1px solid #eaeaeb; } .menu { margin: 0 30px 0 0; } /* link items */ .menu a { clear: right; line-height: 70px; text-decoration: none; margin: 0 10px; text-align: center; color: #33334d; background-color: #ffffff; } .menu a:hover { background-color: #c2c2d6; } /* hamburger properties */ label { float: right; display: none; width: 26px; line-height: 70px; margin: 0 40px 0 0; font-size: 36px; } /* checkbox */ #toggle { } @media only screen and (max-width: 1075px) { /* hamburger properties */ label { display: block; cursor: pointer; } /* nav menu properties */ .menu { width: 100%; display: none; text-align: center; } /* link items */ .menu a { display: block; margin: 0px; border-bottom: 1px solid #eaeaeb; } /* makes links show when checkbox is checked */ #toggle:checked + .menu { display: block; } } My HTML: 
  <div class="nav"> <label for="toggle">&#9776;</label> <input type="checkbox" id="toggle"/> <div class="menu"> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> </div> </div> 

Javscript may not actually be required, depending on your needs. 根据您的需要,实际上可能不需要Javscript。

If you give the div containing your nav links an ID you can target this with an a tag setting the href to the ID. 如果为包含导航链接的div提供ID,则可以使用将href设置为ID a标签作为目标。 Then you can use the :target selector to change the visibility of our navigation div. 然后,您可以使用:target选择器来更改导航div的可见性。

 /* navigation menu */ .nav { position: absolute; top: 0; left: 0; width: 100%; height: 70px; line-height: 70px; text-align: right; z-index: 10000; background-color: #ffffff; border-bottom: 1px solid #eaeaeb; } .menu { margin: 0 30px 0 0; } /* link items */ .menu a { clear: right; line-height: 70px; text-decoration: none; margin: 0 10px; text-align: center; color: #33334d; background-color: #ffffff; } .toggle { text-decoration: none; color: #33334d; } .menu a:hover { background-color: #c2c2d6; } /* hamburger properties */ .toggle, label { float: right; display: none; width: 26px; line-height: 70px; margin: 0 40px 0 0; font-size: 36px; } /* checkbox */ #toggle {} @media only screen and (max-width: 1075px) { /* hamburger properties */ .toggle, label { display: block; cursor: pointer; } /* nav menu properties */ .menu { width: 100%; display: none; text-align: center; } /* link items */ .menu a { display: block; margin: 0px; border-bottom: 1px solid #eaeaeb; } /* makes links show when checkbox is checked */ #menu:target, #toggle:checked+.menu { display: block; } } 
 <div class="nav"> <a class="toggle" href="#menu">&#9776;</a> <div class="menu" id="menu"> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> </div> </div> 

Wow, interesting. 哇有趣。 It's a pretty weird practise, what you have, but it could work. 这是一种非常奇怪的做法,但是可以使用。 You can make menu show/hide by input checked. 您可以通过选中输入来使菜单显示/隐藏。 Very interesting. 很有意思。 I have never think of like that. 我从来没有想过那样。

But also you will need a piece of JS code. 但是,您还需要一段JS代码。

By CSS you can handle some basic selector like :hover, :focus, :active etc. In our your case you also make some interesting click event. 通过CSS,您可以处理一些基本的选择器,例如:hover,:focus,:active等。在您的情况下,您还会产生一些有趣的click事件。 But checkbox is not for that purpose. 但是复选框不是用于此目的。

Click and other event are handled by JS (more https://www.w3schools.com/js/js_events.asp ). 点击和其他事件由JS处理(更多https://www.w3schools.com/js/js_events.asp )。

So in our case, we select all links: 因此,在本例中,我们选择所有链接:

var links = document.querySelectorAll('.menu a');

then we have to add click event to every link, which will set our input to checked="false" = close menu. 那么我们必须在每个链接中添加click事件,这会将我们的输入设置为checked="false" =关闭菜单。

This JS code will only work, when selected links are rendered, so you need to put this piece of code to the end of your html file before </body> or use window.onload ... 此JS代码仅在呈现选定的链接时有效,因此您需要将此代码段放在</body>之前的html文件末尾,或使用window.onload ...

 var links = document.querySelectorAll('.menu a'); var linksLength = links.length for(var i = 0; i < linksLength; i++) { links[i].addEventListener('click', function() { document.getElementById('toggle').checked = false; }); } 
 /* navigation menu */ .nav { position: absolute; top: 0; left: 0; width: 100%; height: 70px; line-height: 70px; text-align: right; z-index: 10000; background-color: #ffffff; border-bottom: 1px solid #eaeaeb; } .menu { margin: 0 30px 0 0; } /* link items */ .menu a { clear: right; line-height: 70px; text-decoration: none; margin: 0 10px; text-align: center; color: #33334d; background-color: #ffffff; } .menu a:hover { background-color: #c2c2d6; } /* hamburger properties */ label { float: right; display: none; width: 26px; line-height: 70px; margin: 0 40px 0 0; font-size: 36px; } /* checkbox */ #toggle { } @media only screen and (max-width: 1075px) { /* hamburger properties */ label { display: block; cursor: pointer; } /* nav menu properties */ .menu { width: 100%; display: none; text-align: center; } /* link items */ .menu a { display: block; margin: 0px; border-bottom: 1px solid #eaeaeb; } /* makes links show when checkbox is checked */ #toggle { display: none; } #toggle:checked + .menu { display: block; } } 
 <label class="nav" for="toggle"> <div class="icon">&#9776;</div> <input type="checkbox" id="toggle"/> <div class="menu"> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> <a href="#">example</a> </div> </label> 

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

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