我正試圖重建蘋果網(wǎng)站。
HTML結(jié)構(gòu):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/style.css">
<link rel="stylesheet" href="styles/menu.css">
<link rel="stylesheet" href="styles/header.css">
<link rel="stylesheet" href="styles/page.css">
<title>Apple</title>
</head>
<body>
<header id="header">
<div class="headerContent">
<a class= "menuBtn">
<ion-icon name="menu-outline" class="logoImg"> </ion-icon>
</a>
<a href="">
<ion-icon name="logo-apple" class="logoImg"> </ion-icon>
</a>
<a href="">
<ion-icon name="bag-outline" class="logoImg"> </ion-icon>
</a>
</div>
</header>
<div class="menu">
<div class="menuContent">
<div class="menuInput">
<input type="text" placeholder="Search apple.com">
</div>
<div class="menuItem">
<a href="">Store</a>
</div>
<div class="menuItem">
<a href="">Mac</a>
</div>
<div class="menuItem">
<a href="">iPad</a>
</div>
<div class="menuItem">
<a href="">iPhone</a>
</div>
<div class="menuItem">
<a href="">Watch</a>
</div>
<div class="menuItem">
<a href="">Airpods</a>
</div>
<div class="menuItem">
<a href="">Tv & Home</a>
</div>
<div class="menuItem">
<a href="">Entertainment</a>
</div>
<div class="menuItem">
<a href="">Accessories</a>
</div>
<div class="menuItem">
<a href="">Support</a>
</div>
</div>
</div>
<div class="list">
<div class="listItem">
<img src="image/list/macBookAir.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macBookPro.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/iMac.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macMini.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macStudio.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macPro.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/compare.PNG" alt="">
</div>
</div>
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
</body>
</html>
JavaScript代碼:
const menuBtn = document.querySelector('.menuBtn');
const menu = document.querySelector('.menu');
const header = document.querySelector('#header');
let IsMenuOpen = false;
menuBtn.addEventListener('click', () => {
if(IsMenuOpen === false) {
slideDown();
header.backgroundColor = "black";
IsMenuOpen = true;
}else {
slideUp();
header.backgroundColor = "#333333";
IsMenuOpen = false;
}
})
function slideUp() {
menu.style.transition = "all 0.5s ease-in-out";
menu.style.height = "0px";
}
slideUp();
function slideDown() {
menu.style.transition = "all 0.5s ease-in-out";
menu.style.height = "100%";
}
然后有4個(gè)不同的css代碼。
這是菜單的css代碼
.menu {
position: absolute;
display: flex; /* disponde gli elementi sull asse principale x */
justify-content: center;
width: 100%;
height: 100%;
background-color: black;
overflow: hidden;
margin-top: -5px;}
.menuContent {
margin-top: 55px;
width: 80%}
.menuInput {
width: 100%;
display: flex;
justify-content: center;
border-bottom: 1px solid #424245;
padding-top: 7px;
padding-bottom: 10px;}
.menuInput input{
width: 95%;
height: 30px;
font-size: 18px;
padding: 5px;
background-color: #1d1d1f;
outline: none;
border: none;
border-radius: 10px;
color: white;}
.menuItem {
display:flex ;
align-items: center;
height: 7%;
font-size: 18px;
border-bottom: 1px solid #424245;}
.menuItem a {
text-decoration: none;
color: #9f9f9f;
transition: all ease 0.3s;}
.menuItem a:hover {
color: white;}
通過(guò)點(diǎn)擊左邊的一個(gè)按鈕,它應(yīng)該打開一個(gè)菜單,但它沒有。即使我不點(diǎn)擊,菜單也總是打開的。 如果我單擊它保持不變,我認(rèn)為JS代碼是正確的,因?yàn)槲覜]有得到任何錯(cuò)誤。
我覺得你需要把相關(guān)代碼放在DOMContentLoaded(DOMContentLoaded)里面。加載時(shí)menuBtn最初是未知的。
試試這個(gè):
window.addEventListener("DOMContentLoaded", (event) => {
const menuBtn = document.querySelector('.menuBtn');
const menu = document.querySelector('.menu');
const header = document.querySelector('#header');
let IsMenuOpen = false;
menuBtn.addEventListener('click', () => {
if(IsMenuOpen === false) {
slideDown();
header.backgroundColor = "black";
IsMenuOpen = true;
}else {
slideUp();
header.backgroundColor = "#333333";
IsMenuOpen = false;
}
})
});
您的代碼正在工作,我認(rèn)為js文件沒有加載到您的html中,請(qǐng)確保您已經(jīng)& lt腳本src = & quotindex.js & quot/& gt;在html文件中(指向js或者只是在html中使用內(nèi)聯(lián)js,就像這樣:
<script>
// your js here
</script>
const menuBtn = document.querySelector('.menuBtn');
const menu = document.querySelector('.menu');
const header = document.querySelector('#header');
let IsMenuOpen = false;
menuBtn.addEventListener('click', () => {
if(IsMenuOpen === false) {
slideDown();
header.backgroundColor = "black";
IsMenuOpen = true;
}else {
slideUp();
header.backgroundColor = "#333333";
IsMenuOpen = false;
}
})
function slideUp() {
menu.style.transition = "all 0.5s ease-in-out";
menu.style.height = "0px";
}
slideUp();
function slideDown() {
menu.style.transition = "all 0.5s ease-in-out";
menu.style.height = "100%";
}
.menu {
position: absolute;
display: flex; /* disponde gli elementi sull asse principale x */
justify-content: center;
width: 100%;
height: 100%;
background-color: black;
overflow: hidden;
margin-top: -5px;}
.menuContent {
margin-top: 55px;
width: 80%}
.menuInput {
width: 100%;
display: flex;
justify-content: center;
border-bottom: 1px solid #424245;
padding-top: 7px;
padding-bottom: 10px;}
.menuInput input{
width: 95%;
height: 30px;
font-size: 18px;
padding: 5px;
background-color: #1d1d1f;
outline: none;
border: none;
border-radius: 10px;
color: white;}
.menuItem {
display:flex ;
align-items: center;
height: 7%;
font-size: 18px;
border-bottom: 1px solid #424245;}
.menuItem a {
text-decoration: none;
color: #9f9f9f;
transition: all ease 0.3s;}
.menuItem a:hover {
color: white;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles/style.css">
<link rel="stylesheet" href="styles/menu.css">
<link rel="stylesheet" href="styles/header.css">
<link rel="stylesheet" href="styles/page.css">
<title>Apple</title>
</head>
<body>
<header id="header">
<div class="headerContent">
<a class= "menuBtn">
<ion-icon name="menu-outline" class="logoImg"> </ion-icon>
</a>
<a href="#">
<ion-icon name="logo-apple" class="logoImg"> </ion-icon>
</a>
<a href="#">
<ion-icon name="bag-outline" class="logoImg"> </ion-icon>
</a>
</div>
</header>
<div class="menu">
<div class="menuContent">
<div class="menuInput">
<input type="text" placeholder="Search apple.com">
</div>
<div class="menuItem">
<a href="">Store</a>
</div>
<div class="menuItem">
<a href="">Mac</a>
</div>
<div class="menuItem">
<a href="">iPad</a>
</div>
<div class="menuItem">
<a href="">iPhone</a>
</div>
<div class="menuItem">
<a href="">Watch</a>
</div>
<div class="menuItem">
<a href="">Airpods</a>
</div>
<div class="menuItem">
<a href="">Tv & Home</a>
</div>
<div class="menuItem">
<a href="">Entertainment</a>
</div>
<div class="menuItem">
<a href="">Accessories</a>
</div>
<div class="menuItem">
<a href="">Support</a>
</div>
</div>
</div>
<div class="list">
<div class="listItem">
<img src="image/list/macBookAir.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macBookPro.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/iMac.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macMini.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macStudio.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/macPro.PNG" alt="">
</div>
<div class="listItem">
<img src="image/list/compare.PNG" alt="">
</div>
</div>
<script type="module" src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons@7.1.0/dist/ionicons/ionicons.js"></script>
</body>
</html>