gpt4 book ai didi

javascript - "Uncaught TypeError: Cannot read property ' 添加事件监听器 ' of null"

转载 作者:行者123 更新时间:2023-11-28 05:11:00 24 4
gpt4 key购买 nike

我试图用一些点 HTML 和 CSS 制作一个导航开启器,但脚本无法正常工作。非常感谢任何帮助。

我的代码:

const burger = document.querySelector(".burger");
const navLinks = document.querySelector(".item-menu");
const links = document.querySelectorAll(".list");


burger.addEventListener("click", () => {
navLinks.classList.toggle("open");
});
* {
margin: 0px;
padding: 0;
}

body {
font-family: cursive, sans-serif, fantasy;
background-color: #f3f3f4
}

nav {
height: 12vh;
background-color: #0303;
display: block;
}

.item-menu {
display: flex;
list-style: none;
justify-content: space-around;
align-items: center;
width: 50%;
height: 100%;
margin-left: auto;
cursor: pointer;
}

.a
/* menu words */

{
text-decoration: none;
list-style: none;
color: black;
}


/*---------------------------------PHONE---------------------------------*/

@media only screen and (max-width: 750px) {
/* BURGER NAV DOTS */
.dot {
float: right;
display: block;
height: 25px;
width: 25px;
border-radius: 50%;
}
#burger:hover {
cursor: pointer;
}
#burger:hover .dot:nth-child(1) {
box-shadow: 0 0 10px #fff900;
}
#burger:hover .dot:nth-child(2) {
box-shadow: 0 0 10px #08ff28;
}
#burger:hover .dot:nth-child(3) {
box-shadow: 0 0 10px #ff005a;
}
#burger:hover .dot:nth-child(4) {
box-shadow: 0 0 10px #3896ff;
}
/* DOTS */
#burger .dot:nth-child(1) {
background-color: #fff900;
animation: animate 2s linear inifinite;
}
#burger .dot:nth-child(2) {
background-color: #08ff28;
animation: animate 2s linear inifinite;
}
#burger .dot:nth-child(3) {
background-color: #ff005a;
animation: animate 2s linear inifinite;
}
#burger .dot:nth-child(4) {
background-color: #3896ff;
animation: animate 2s linear inifinite;
}
#burger {
position: absolute;
cursor: pointer;
right: 5%;
top: 50%;
transform: translate(-5%, -50%);
z-index: 2;
width: 50px;
height: 50px;
}
nav {
position: relative;
}
.item-menu {
position: fixed;
top: 2%;
left: -5%;
height: 85vh;
width: 100%;
clip-path: inset(0px 0px 692px 612px);
-webkit-clip-path: inset(0px 0px 692px 612px);
pointer-events: none;
cursor: pointer;
display: flex;
width: 84%;
height: 84%;
transition: all 1s ease-out;
align-items: center;
}
.a {
position: absolute;
top: 3%;
}
.item-menu.open {
clip-path: inset(0px 0px 692px 0px);
-webkit-clip-path: inset(0px 0px 692px 0px);
transition: all 1s ease-out;
pointer-events: all;
}
}
<!DOCTYPE html>
<html>

<head>
<title> BURGER NAV </title>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--STYLESHEET-->
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<nav>
<div id="burger">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>

<ul class="item-menu">
<li class="list"><a class="a" href=""> Home </a></li>
<li class="list"><a class="a" href=""> Portfoglio </a></li>
<li class="list"><a class="a" href=""> Experiences </a></li>
<li class="list"><a class="a" href=""> About me </a></li>
<li class="list"><a class="a" href=""> Contact </a></li>
</ul>

</nav>

</body>

</html>

我的问题:

嗯,看起来按钮不能正常工作,我不明白为什么。他们告诉我脚本没有加载并把它放在主体的末尾并且已经加载了。我试图更改一些类,添加更多类,执行一个函数……以上都没有用……

拜托,我需要帮助...我快要崩溃了:)) =)) 也许编码不适合我 :X

最佳答案

在 'burger' 的 querySelector 中将其设置为const burger = document.querySelector("#burger");这是一个 id,所以我们使用“#”点“。”是给类(class)的

关于javascript - "Uncaught TypeError: Cannot read property ' 添加事件监听器 ' of null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56013233/

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