我正在尝试建立我的第一个网站,我已经被这个问题困扰了两天。当我调整窗口大小时或在移动设备上使用网站时,我想创建一个带有汉堡菜单的响应式导航栏。但是,当我单击汉堡包菜单时,没有任何反应,菜单不显示。谁能帮我找出错误:(?如果您需要更多详细信息,请告诉我,非常感谢!
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="scripts.js" defer></script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
<link
href="https://fonts.googleapis.com/css2?family=Lora&display=swap"
rel="stylesheet"
/>
<title>Myfirstwebsite</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<nav class="container">
<!-- Logo -->
<div class="logo">
<a href="index.html">
<img
src="images/logo.png"
alt=" Myfirstwebsitelogo"
class="nav-logo"
/>
Myfirstwebsite
</a>
</div>
<!-- Navigation Menu -->
<ul class="menu">
<li><a href="about.html">About</a></li>
<li><a href="articles.html">Articles</a></li>
<li><a href="use-cases.html">Use Cases</a></li>
<li><a href="resources.html">Resources</a></li>
<li><a href="blog.html">Blog</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
<!-- Hamburger Icon -->
<div class="hamburger-menu">
<span></span>
<span></span>
<span></span>
</div>
<!-- Mobile Navigation Menu -->
<ul class="mobile-menu">
<li><a href="about.html">About</a></li>
<li><a href="articles.html">Articles</a></li>
<li><a href="use-cases.html">Use Cases</a></li>
<li><a href="resources.html">Resources</a></li>
<li><a href="blog.html">Blog</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>
<footer>
<!-- Add your footer content here -->
</footer>
<script src="scripts.js"></script>
</body>
</html>
样式.css
/* Header */
header {
width: 100%;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
nav {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
.nav-logo {
max-width: 50px;
max-height: 50px;
margin-right: 8px;
vertical-align: middle;
}
/* Navigation Menu */
.menu {
list-style: none;
display: flex;
align-items: center;
}
.menu li a {
text-decoration: none;
color: #111131;
padding: 0.5rem 1rem;
display: inline-block;
}
.menu li a:hover {
background-color: #ff3d0d;
color: #fff;
border-radius: 4px;
}
/* Main Content */
main {
padding: 2rem;
}
/* Container */
.container {
max-width: 1000px; /* Adjust this value to match the desired width */
margin: 0 auto; /* Center the container */
padding: 0 1rem; /* Add padding for smaller screens */
}
/* Hamburger Icon */
.hamburger-menu {
display: none;
flex-direction: column;
justify-content: space-between;
width: 25px;
height: 15px;
cursor: pointer;
}
.hamburger-menu span {
width: 100%;
height: 3px;
background-color: #111131;
}
/* Mobile Navigation Menu */
.mobile-menu {
display: none;
list-style: none;
position: absolute;
top: 100%;
left: 0;
background-color: #fff;
width: 100%;
padding: 1rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
z-index: 999;
}
.mobile-menu li {
margin-bottom: 1rem;
}
.mobile-menu li a {
text-decoration: none;
color: #111131;
font-size: 1rem;
}
.mobile-menu li a:hover {
color: #5c5cff;
}
/* Responsive styles */
@media screen and (max-width: 1024px) {
.hamburger-menu {
display: flex;
}
.menu {
display: none;
}
/* Show mobile menu when it's active */
.mobile-menu.active {
display: block;
}
}
.mobile-menu {
background-color: #fff;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.hamburger-menu.active span:nth-child(1) {
transform: translateY(3px) rotate(45deg);
}
.hamburger-menu.active span:nth-child(2) {
opacity: 0;
}
.hamburger-menu.active span:nth-child
/* this CSS makes an article picture responsive, In my articles.html, wrap the image with a <div> element using the class .article-image: */
.article-image img {
width: 100%;
height: auto;
max-width: 768px; /* Limit the maximum width of the image */
display: block; /* Remove inline spacing */
margin: 0 auto; /* Center the image when the container is wider */
}
scripts.js
document.addEventListener('DOMContentLoaded', function () {
// Get the hamburger menu element
const hamburgerMenu = document.querySelector('.hamburger-menu');
// Get the mobile menu element
const mobileMenu = document.querySelector('.mobile-menu');
// Toggle the active class on both the hamburger menu and mobile menu
function toggleMenu() {
hamburgerMenu.classList.toggle('active');
mobileMenu.classList.toggle('active');
}
// Add a click event listener to the hamburger menu
hamburgerMenu.addEventListener('click', toggleMenu);
// Add a click event listener to each mobile menu link to close the menu when a link is clicked
mobileMenu.querySelectorAll('a').forEach(function (menuItem) {
menuItem.addEventListener('click', toggleMenu);
});
});
我尝试了上面的代码,我期待看到一个响应式导航栏可视化我在全屏测试网站时正确看到的菜单。但是,当我调整屏幕大小时,汉堡包图标正确显示,但当我单击时没有任何反应。再次感谢您的宝贵时间和帮助!
你加载了两次 JS 文件,所以事件监听器也被添加了两次。结果,第一个事件处理程序将
.active
类名添加到元素中,但第二个事件处理程序立即将其删除。虽然元素在 DOM 中更新(您可以在浏览器的 devTools 中检查此行为),但最后没有添加类名。
要解决此问题,只需从 HTML 中删除其中一个脚本标签即可。