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

问题描述 投票:0回答:1

所以单击链接时,我在关闭移动导航汉堡菜单时遇到问题。仅供参考,现在只能单击我的顶部链接。

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

我看了很多,但是我能找到的只是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');
    });
});
javascript function click
1个回答
0
投票

您正在监听的唯一点击是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');
  });
})

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

© www.soinside.com 2019 - 2024. All rights reserved.