Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击链接后使导航菜单消失的问题_Javascript_Function_Click - Fatal编程技术网

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

Javascript 单击链接后使导航菜单消失的问题,javascript,function,click,Javascript,Function,Click,因此,当点击链接时,我在关闭我的移动导航汉堡菜单时遇到了问题。现在只有我的顶部链接可点击仅供参考 答案就在嘴边,但我还不太清楚。我认为这主要是我这边的语法问题。因此,是的,移动设备上的菜单在单击菜单时会打开和关闭,但在单击链接时不会关闭 我找遍了所有地方,但我能找到的都是jQuery的答案 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta

因此,当点击链接时,我在关闭我的移动导航汉堡菜单时遇到了问题。现在只有我的顶部链接可点击仅供参考

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

我找遍了所有地方,但我能找到的都是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>

你唯一听到的点击是
汉堡
元素

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

const hamburger=document.querySelector('.hamburger');
const navLinks=document.querySelector('.nav links');
const links=document.queryselectoral('.nav links li');
constwhater=document.queryselectoral('.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');
});
})

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

ahhh是的,谢谢。昨晚我尝试了类似的方法,但我为它创建了一个新变量,并遇到了一个“.addEventListener不是一个函数”我最喜欢的错误
@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');
    });
});