admin管理员组文章数量:1026174
I want to close my menu by clicking on a link or when I click outside the menu. In the interest of keeping things nice and light, I don't want to use jQuery.
How do I do it?
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
I want to close my menu by clicking on a link or when I click outside the menu. In the interest of keeping things nice and light, I don't want to use jQuery.
How do I do it?
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
Share
Improve this question
edited Feb 14, 2017 at 7:38
Pang
10.1k146 gold badges86 silver badges124 bronze badges
asked Feb 13, 2017 at 9:55
DenisMasotDenisMasot
7475 gold badges12 silver badges21 bronze badges
2
- Hope this will help link – Souad Commented Feb 13, 2017 at 9:56
- here's something that might interest you: Javascript on click hide – Jeremy Comelli Commented Feb 13, 2017 at 9:58
2 Answers
Reset to default 3It can be done with DOM API using addEventListener() on document. You then need logic to find if the click target was on the menu or any of its elements, or somewhere else on the page.
isDescendent()
borrowed from How to check in Javascript if one element is contained within another
The menu can then be hidden with element.style.display = 'none'
I would question motivation to not use jQuery, you'll be making a lot more work for yourself in the long run...
var menu = document.querySelector("nav.nav");
var checkbox = document.querySelector("input[type=checkbox]");
document.addEventListener("click", function(e) {
if (menu != e.target &&
! isDescendant(menu, e.target)) {
console.log("Clicked somewhere else");
menu.style.display = 'none';
checkbox.checked = false;
}
}, false);
function isDescendant(parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
}
<div>
Rest of page...
</div>
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
one way, hope it helps
$(document).ready(function() {
$('.click').click(function() {
$('.nav__right').slideToggle("fast");
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<button class="click">click me</button>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
I want to close my menu by clicking on a link or when I click outside the menu. In the interest of keeping things nice and light, I don't want to use jQuery.
How do I do it?
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
I want to close my menu by clicking on a link or when I click outside the menu. In the interest of keeping things nice and light, I don't want to use jQuery.
How do I do it?
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
Share
Improve this question
edited Feb 14, 2017 at 7:38
Pang
10.1k146 gold badges86 silver badges124 bronze badges
asked Feb 13, 2017 at 9:55
DenisMasotDenisMasot
7475 gold badges12 silver badges21 bronze badges
2
- Hope this will help link – Souad Commented Feb 13, 2017 at 9:56
- here's something that might interest you: Javascript on click hide – Jeremy Comelli Commented Feb 13, 2017 at 9:58
2 Answers
Reset to default 3It can be done with DOM API using addEventListener() on document. You then need logic to find if the click target was on the menu or any of its elements, or somewhere else on the page.
isDescendent()
borrowed from How to check in Javascript if one element is contained within another
The menu can then be hidden with element.style.display = 'none'
I would question motivation to not use jQuery, you'll be making a lot more work for yourself in the long run...
var menu = document.querySelector("nav.nav");
var checkbox = document.querySelector("input[type=checkbox]");
document.addEventListener("click", function(e) {
if (menu != e.target &&
! isDescendant(menu, e.target)) {
console.log("Clicked somewhere else");
menu.style.display = 'none';
checkbox.checked = false;
}
}, false);
function isDescendant(parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
}
<div>
Rest of page...
</div>
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
one way, hope it helps
$(document).ready(function() {
$('.click').click(function() {
$('.nav__right').slideToggle("fast");
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<nav class="nav">
<div class="nav__left">
<h3><a href="#home">DenisMasot</a></h3>
</div>
<input type="checkbox" /><label class="burger" for="nav"></label>
<button class="click">click me</button>
<ul class="nav__right">
<li class="active">
<h3><a href="#home">home</a></h3>
</li>
<li>
<h3><a href="#about">À propos</a></h3>
</li>
<li>
<h3><a href="#production">Réalisation</a></h3>
</li>
<li>
<h3><a href="#contact">Contact</a></h3>
</li>
</ul>
</nav>
本文标签: javascriptClose the menu on clickStack Overflow
版权声明:本文标题:javascript - Close the menu on click - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636688a2160492.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论