gpt4 book ai didi

单击任何选项时关闭菜单的 JavaScript 或 CSS 函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:45:45 25 4
gpt4 key购买 nike

我不知道如何在单击或选择元素时关闭菜单。 muenu 仅是 CSS,但我添加 JavaScript 代码没有问题,尽管我更愿意将其保留为仅 CSS

https://codepen.io/nigarcia/pen/wQdVBW

<div class="navigation">
<input type="checkbox" class="nav-check" id="navi-toggle">

<label for="navi-toggle" class="nav-btn">
<span class="nav-icon">&nbsp;</span>
</label>

<div class="nav-bg">&nbsp;</div>

<nav class="nav-nav">
<ul class="nav-list">
<li class="nav-item"><a href="#" class="nav-link">Home</a></li>
<li class="nav-item"><a href="#" class="nav-link">About</a></li>
<li class="nav-item"><a href="#" class="nav-link">Services</a></li>
<li class="nav-item"><a href="#" class="nav-link">Contact</a></li>
</ul>
</nav>
</div>

最佳答案

这是一个 JavaScript 解决方案。所有代码都有注释。

var elementsArray = document.querySelectorAll('.nav-link'); /* create an array of your '.nav-link' li */

elementsArray.forEach(function(myBtn) {
/*add a listener to every element of this array */
myBtn.addEventListener("click", function(e) {
e.preventDefault()/* prevent default link behaviour */
document.getElementById("navi-toggle").checked = false; /* unchecked your checkbox */
});
});
.nav-check {
display: none;
}

.nav-btn {
background-color: #fff;
height: 4rem;
width: 4rem;
position: absolute;
top: 2rem;
left: 2rem;
border-radius: 50%;
z-index: 2000;
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.1);
text-align: center;
cursor: pointer;
}

.nav-bg {
height: 3.8rem;
width: 3.8rem;
border-radius: 50%;
position: absolute;
top: 2rem;
left: 2rem;
background-image: linear-gradient(rgb(224, 224, 224), rgba(236, 236, 236, 0.9));
z-index: 1000;
transition: transform 0.8s cubic-bezier(0.86, 0, 0.07, 1);
}

.nav-nav {
height: 100vh;
font-family: Northshire Script, sans-serif;
position: fixed;
top: 0;
left: 0;
z-index: 1500;
opacity: 0;
width: 0;
transition: all 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.nav-list {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
list-style: none;
text-align: center;
width: 100%;
}

.nav-item {
margin: 1rem;
}

.nav-link:link, .nav-link:visited {
display: inline-block;
font-size: 7rem;
padding: 1rem 1rem;
color: rgb(0, 0, 0);
text-decoration: none;
transition: all .4s;
}
.nav-link:link span, .nav-link:visited span {
margin-right: 1.5rem;
display: inline-block;
}

.nav-link:hover, .nav-link:active {
background-position: 100%;
color: #55c57a;
transform: translateX(1rem);
}

.nav-check:checked ~ .nav-bg {
transform: scale(80);
overflow: hidden;
}

.nav-check:checked ~ .nav-nav {
opacity: 1;
width: 100%;
}

.nav-check::after ~ .nav-bg {
transform: scale(0);
}

.nav-icon {
position: relative;
margin-top: 1.9rem;
}
.nav-icon, .nav-icon::before, .nav-icon::after {
width: 2rem;
height: 5px;
background-color: #333;
display: inline-block;
}
.nav-icon::before, .nav-icon::after {
content: "";
position: absolute;
left: 0;
transition: all .2s;
}
.nav-icon::before {
top: -.8rem;
}
.nav-icon::after {
top: .8rem;
}

.nav-btn:hover .nav-icon::before {
top: -1rem;
}

.nav-btn:hover .nav-icon::after {
top: 1rem;
}

.nav-check:checked + .nav-btn .nav-icon {
background-color: transparent;
}

.nav-check:checked + .nav-btn .nav-icon::before {
transform: rotate(135deg);
top: 0;
}

.nav-check:checked + .nav-btn .nav-icon::after {
top: 0;
transform: rotate(-135deg);
}
<div class="navigation">
<input type="checkbox" class="nav-check" id="navi-toggle">

<label for="navi-toggle" class="nav-btn">
<span class="nav-icon">&nbsp;</span>
</label>

<div class="nav-bg">&nbsp;</div>

<nav class="nav-nav">
<ul class="nav-list">
<li class="nav-item"><a href="#" class="nav-link">Home</a></li>
<li class="nav-item"><a href="#" class="nav-link">About</a></li>
<li class="nav-item"><a href="#" class="nav-link">Services</a></li>
<li class="nav-item"><a href="#" class="nav-link">Contact</a></li>
</ul>
</nav>
</div>

关于单击任何选项时关闭菜单的 JavaScript 或 CSS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53323984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com