gpt4 book ai didi

javascript - 单击链接后导航菜单消失的问题

转载 作者:行者123 更新时间:2023-12-02 22:21:21 24 4
gpt4 key购买 nike

所以我在单击链接时关闭移动导航汉堡菜单时遇到问题。目前只有我的顶部链接可以点击,仅供引用。

答案就在我的舌尖上,但我还不太清楚。我认为这主要是我的语法问题。因此,是的,移动设备上的菜单在单击菜单时打开和关闭,但在单击链接时不会关闭。

我已经查遍了,但我能找到的只是 jQuery 的答案。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css\style.css">
<title>eddiepearsonUX</title>
</head>
<body>
<nav>
<div class="hamburger">
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>
<ul class="nav-links">
<li><a href="#Audio-UX-Study">Audio UX Study</a></li>
<li><a href="#">Web App UX</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>

<section class="intro-section">
<h1 class="name">
<span>eddie</span>
<span>Pearson</span>
</h1>
<h3 class="intro">
<p>Audio<br>and Visual</p>
<p>UX</p>
</h3>
</section>

<h3 class="content-title">
<p>Audio UX Study</p>
</h3>

<ul style="list-style-type: none" id="Audio-UX-Study"class="content">
<li class="main-img"><img src="https://www.dl.dropboxusercontent.com/s/rktj2nhj07l2ne7/20191206-Screenshot%20%28223%29.jpg?dl=0" alt="Ableton screen with wavforms and effects stack"></li>
<li class="second-img"><img src="https://www.dl.dropboxusercontent.com/s/j1aipb71ccj3o64/Screenshot%20%28225%29.png?dl=0" alt="Wavforms from audio"></li>
<li class="copy">

</li>
<li class="main-img"></li>
<li class="second-img"></li>
<li class="copy">

</li>
</ul>
<script src="js\app.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css?family=Merriweather:300,700&display=swap');

* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: 'Merriweather', serif;
}

body {
font-family: sans-serif;
background-color: #e6e6e0;
}

/* NAVIGATION */

nav {
height: 10vh;
background: #e6e6e0;
/* position: sticky; */
}

.name-div {
position: absolute;
width: 50%;
top: 1.8rem;
left: 2rem;
}

.name {
color: rgb(82, 82, 56);
font-size: calc(0.2rem + 1.1rem);
}

.nav-links {
display: flex;
position: relative;
list-style: none;
max-width: 75vw;
height: 100%;
justify-content: end;
align-items: center;
margin: auto;
}

.nav-links li a {
color:rgb(82, 82, 56);
font-weight: bold;
text-decoration: none;
font-size: 18px;
padding-left: 1em;
}

@media screen and (max-width: 768px) {
.nav-links {
position: fixed;
background: #e6e6e0;
height: 100vh;
width: 100%;
flex-direction: column;
justify-content: space-around;
margin-left: auto;
clip-path: circle(100px at 90% -20%);
-webkit-clip-path: circle(100px at 90% -10%);
transition: all 1s ease-out;
pointer-events: none;
}

.nav-links.open {
clip-path: circle(1100px at 90% -10%);
-webkit-clip-path: circle(1100px at 90% -10%);
pointer-events: all;
max-width: 95%;
}

.line {
width: 30px;
height: 3px;
background: rgb(82, 82, 56);
margin: 5px;
}

nav {
position: relative;
}

.hamburger {
position: absolute;
cursor: pointer;
right: 5%;
top: 50%;
transform: translate(-5%, -50%);
z-index: 2;
}

.nav-links li {
opacity: 0;
}

.nav-links a {
font-size: 25px;
}

.nav-links li:nth-child(1) {
transition: all 0.5s ease 0.1s;
}

.nav-links li:nth-child(2) {
transition: all 0.5s ease 0.2s;
}

.nav-links li:nth-child(3) {
transition: all 0.5s ease 0.3s;
}

.nav-links li:nth-child(4) {
transition: all 0.5s ease 0.4s;
}

li.fade {
opacity: 1;
}
}

/* INTRO SECTION */

.intro-section {
max-width: 75vw;
margin: auto;
/* background-color: #fff; */
}

.intro-section .name {
padding: 2rem 0 2rem 0rem;
font-size: calc(0.8rem + 3vw);
}

.intro-section .name span:nth-of-type(1) {
color: rgb(174, 177, 156);
}

.intro {
color:rgb(174, 177, 156);
font-size: calc(0.6rem + 2vw);
}

.intro p:nth-of-type(2) {
font-size: calc(0.6rem + 3vw);
color:rgb(82, 82, 56);
}

/* CONTENT SECTION */

.content-title {
display: block;
margin: auto;
margin-top: 5rem;
margin-bottom: 2rem;
max-width: 75vw;
font-size: calc(0.6rem + 1vw);
color:rgb(82, 82, 56);
}

.content {
max-width: 75vw;
margin: 3rem auto;
display: grid;
width: 100%;
height: auto;
display: grid;
margin-bottom: 3em;
grid-gap: 1em;
grid-template-columns: repeat(2, 1fr);
grid-auto-rows: minmax(100px, auto);
}

.main-img {
grid-column: 1 / 2;
}


.content > li > img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}

const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelectorAll('.nav-links li');
const whatever = document.querySelectorAll('.nav-links li a');

hamburger.addEventListener('click', () => {
navLinks.classList.toggle('open');
links.forEach(link => {
link.classList.toggle('fade');
});
});

最佳答案

您监听点击的唯一内容是 hamburger 元素。

尝试在 navLinks 元素上添加监听器,如下所示:

const hamburger = document.querySelector('.hamburger');
const navLinks = document.querySelector('.nav-links');
const links = document.querySelectorAll('.nav-links li');
const whatever = document.querySelectorAll('.nav-links li a');

hamburger.addEventListener('click', () => {
navLinks.classList.toggle('open');
links.forEach(link => {
link.classList.toggle('fade');
});
});

navLinks.addEventListener('click', () => {
navLinks.classList.toggle('open')
links.forEach(link => {
link.classList.toggle('fade');
});
})

您可能需要进行更多调整,但这应该可以帮助您开始。

关于javascript - 单击链接后导航菜单消失的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219409/

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