当我点击菜单中的 a 时关闭我的 span
Close my spans when I click on my a inside my menu
几天前我制作了一个响应式菜单,它运行良好,除了当我在我创建的 menu-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-attribute 到 input[type="checkbox"]:
<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>
几天前我制作了一个响应式菜单,它运行良好,除了当我在我创建的 menu-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-attribute 到 input[type="checkbox"]:
<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>