几天前我制作了一个响应菜单,它运行得很好,只是当我在我创建的菜单ul中单击a时,它会关闭菜单,但不会关闭汉堡菜单的状态。
这是代码,所以你可以理解
function openNav(){
document.getElementById('menu-ul').classList.toggle("open");
}
body {
display: grid;
}
#back-top {
position: fixed;
bottom: 40px;
right: 14px;
z-index: 9995;
width: 35px;
height: 35px;
text-align: center;
font-size: 45px;
font-family: 'Agency FB', arial;
line-height: 32px;
background: #22cfb5;
color: #fff;
cursor: pointer;
border-radius: 50%;
transform: rotate(-90deg);
-o-transition:background-color .5s;
-ms-transition:background-color .5s;
-moz-transition:background-color .5s;
-webkit-transition:background-color .5s;
transition:background-color .5s;
text-decoration: none;
}
#back-top:hover {
background: #4c4bbf;
}
header {
position: sticky;
top: 0;
z-index: 9999;
}
#navcontainer {
background-color: #4c4bbf;
}
.menu span {
display: block;
width: 19px;
height: 2px;
margin-bottom: 3px;
position: relative;
background: #ffffff;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s
cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s
cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
transition-property: transform, background, opacity;
transition-duration: 0.5s, 0.5s, 0.55s;
transition-timing-function:
cubic-bezier(0.77, 0.2, 0.05, 1),
cubic-bezier(0.77, 0.2, 0.05, 1),
ease;
transition-delay: 0s, 0s, 0s;
}
.menu input {
display: block;
width: 19px;
height: 13px;
position: absolute;
cursor: pointer;
opacity: 0;
z-index: 2;
}
.menu input:checked~span {
opacity: 1;
transform: rotate(-45deg) translate(-2px, -1px);
}
.menu input:checked~span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
.menu input:checked~span:nth-last-child(2) {
transform: rotate(45deg) translate(-2px, -1px);
}
@media (max-width: 767px) {
body {
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 74.375rem 11.1875rem;
gap: 0px 20px;
}
header {
grid-column: 1 / span 6;
grid-row: 1 / span 1;
}
#navcontainer {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem;
column-gap: 10px;
place-items: center;
}
.logo {
grid-column: 1 / span 5;
}
.menu {
grid-column: 6 / span 1;
}
#menu-ul {
background: linear-gradient(-30deg,#6633ff,#6666ff);
z-index: 9998;
grid-column: 1 / span 6;
grid-row: 1 / span 7;
position: absolute;
width: 100%;
left: -767px;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 74.375rem 11.1875rem;
gap: 0px 20px;
transition: transform .3s ease-in-out;
}
#menu-ul.open {
left: 0px;
}
ul {
grid-column: 2 / span 4;
grid-row: 1 / span 3;
list-style-type: none;
-webkit-font-smoothing: antialiased;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem
4.375rem;
place-items: center;
}
.menu input:checked~#menu-ul {
transform: translateX(767px);
}
.a-menu1 {
grid-column: 2 / span 4;
grid-row: 1 / span 3;
}
.a-menu2 {
grid-column: 2 / span 4;
grid-row: 2 / span 3;
}
.a-menu3 {
grid-column: 2 / span 4;
grid-row: 3 / span 3;
}
.a-menu4 {
grid-column: 2 / span 4;
grid-row: 4 / span 3;
}
#linkedin-menu {
height: 28px;
width: 28px;
grid-column: 1 / span 3;
grid-row: 6 / span 2;
}
#insta-menu {
height: 28px;
width: 28px;
grid-column: 4 / span 3;
grid-row: 6 / span 2;
}
}
<body>
<a href="#" id="back-top" title="Back top">></a>
<header>
<nav role="navigation" id="navcontainer">
<div class="logo">
<img src="../logo_249.png">
</div>
<div class="menu">
<input type="checkbox" onclick="openNav()"/>
<span id="span1"></span>
<span id="span2"></span>
<span id="span3"></span>
</div>
</nav>
</header>
<div id="menu-ul">
<ul>
<a class="a-menu1" href="#bienvenue" onclick="openNav()"><li>À propos</li></a>
<a class="a-menu2" href="#services" onclick="openNav()"><li>Mes services</li></a>
<a class="a-menu3" href="#projets" onclick="openNav()"><li>Mes projets</li></a>
<a class="a-menu4" href="#contact" onclick="openNav()"><li>Contact</li></a>
<img src="img/linkedin-white.png" id="linkedin-menu">
<img src="img/insta-white.png" id="insta-menu">
</ul>
</div>
</body>
有人能帮我吗?我已经检查了一些与我相近的科目,但没有找到解决问题的答案。非常感谢!:(
这是因为如果您单击菜单项,您的复选框没有被取消选中。你可以这样修改功能:
function openNav(){
const menuUl = document.getElementById('menu-ul');
const burger = document.getElementById('burger');
if (menuUl.classList.contains('open')) {
burger.checked = false;
}
menuUl.classList.toggle('open');
}
并将id属性添加到输入[类型="复选框"]:
<input id="burger" type="checkbox" onclick="openNav()"/>
我希望,这将帮助你!
您可以通过以下小的更改来实现这一点。
步骤1:创建一个函数toggleMenu((来切换负责动画的复选框。
步骤2:将上述函数添加到所有锚点标签中,作为onclick="toggleMenu((">。
请查看演示
function openNav(){
document.getElementById('menu-ul').classList.toggle("open");
}
function toggleMenu() {
document.getElementsByClassName('menu')[0].getElementsByTagName('input')[0].checked = !document.getElementsByClassName('menu')[0].getElementsByTagName('input')[0].checked;
openNav();
}
body {
display: grid;
}
#back-top {
position: fixed;
bottom: 40px;
right: 14px;
z-index: 9995;
width: 35px;
height: 35px;
text-align: center;
font-size: 45px;
font-family: 'Agency FB', arial;
line-height: 32px;
background: #22cfb5;
color: #fff;
cursor: pointer;
border-radius: 50%;
transform: rotate(-90deg);
-o-transition:background-color .5s;
-ms-transition:background-color .5s;
-moz-transition:background-color .5s;
-webkit-transition:background-color .5s;
transition:background-color .5s;
text-decoration: none;
}
#back-top:hover {
background: #4c4bbf;
}
header {
position: sticky;
top: 0;
z-index: 9999;
}
#navcontainer {
background-color: #4c4bbf;
}
.menu span {
display: block;
width: 19px;
height: 2px;
margin-bottom: 3px;
position: relative;
background: #ffffff;
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s
cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s
cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
transition-property: transform, background, opacity;
transition-duration: 0.5s, 0.5s, 0.55s;
transition-timing-function:
cubic-bezier(0.77, 0.2, 0.05, 1),
cubic-bezier(0.77, 0.2, 0.05, 1),
ease;
transition-delay: 0s, 0s, 0s;
}
.menu input {
display: block;
width: 19px;
height: 13px;
position: absolute;
cursor: pointer;
opacity: 0;
z-index: 2;
}
.menu input:checked~span {
opacity: 1;
transform: rotate(-45deg) translate(-2px, -1px);
}
.menu input:checked~span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
.menu input:checked~span:nth-last-child(2) {
transform: rotate(45deg) translate(-2px, -1px);
}
@media (max-width: 767px) {
body {
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 74.375rem 11.1875rem;
gap: 0px 20px;
}
header {
grid-column: 1 / span 6;
grid-row: 1 / span 1;
}
#navcontainer {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem;
column-gap: 10px;
place-items: center;
}
.logo {
grid-column: 1 / span 5;
}
.menu {
grid-column: 6 / span 1;
}
#menu-ul {
background: linear-gradient(-30deg,#6633ff,#6666ff);
z-index: 9998;
grid-column: 1 / span 6;
grid-row: 1 / span 7;
position: absolute;
width: 100%;
left: -767px;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 74.375rem 11.1875rem;
gap: 0px 20px;
transition: transform .3s ease-in-out;
}
#menu-ul.open {
left: 0px;
}
ul {
grid-column: 2 / span 4;
grid-row: 1 / span 3;
list-style-type: none;
-webkit-font-smoothing: antialiased;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem
4.375rem;
place-items: center;
}
.menu input:checked~#menu-ul {
transform: translateX(767px);
}
.a-menu1 {
grid-column: 2 / span 4;
grid-row: 1 / span 3;
}
.a-menu2 {
grid-column: 2 / span 4;
grid-row: 2 / span 3;
}
.a-menu3 {
grid-column: 2 / span 4;
grid-row: 3 / span 3;
}
.a-menu4 {
grid-column: 2 / span 4;
grid-row: 4 / span 3;
}
#linkedin-menu {
height: 28px;
width: 28px;
grid-column: 1 / span 3;
grid-row: 6 / span 2;
}
#insta-menu {
height: 28px;
width: 28px;
grid-column: 4 / span 3;
grid-row: 6 / span 2;
}
}
<body>
<a href="#" id="back-top" title="Back top">></a>
<header>
<nav role="navigation" id="navcontainer">
<div class="logo">
<img src="../logo_249.png">
</div>
<div class="menu">
<input type="checkbox" onclick="openNav()"/>
<span id="span1"></span>
<span id="span2"></span>
<span id="span3"></span>
</div>
</nav>
</header>
<div id="menu-ul">
<ul>
<a class="a-menu1" href="#bienvenue" onclick="toggleMenu()"><li>À propos</li></a>
<a class="a-menu2" href="#services" onclick="toggleMenu()"><li>Mes services</li></a>
<a class="a-menu3" href="#projets" onclick="toggleMenu()"><li>Mes projets</li></a>
<a class="a-menu4" href="#contact" onclick="toggleMenu()"><li>Contact</li></a>
<img src="img/linkedin-white.png" id="linkedin-menu">
<img src="img/insta-white.png" id="insta-menu">
</ul>
</div>
</body>