我正在尝试建立我的第一个网站,一周以来我一直被这个问题困扰。当我调整窗口大小时或在移动设备上使用网站时,我想创建一个带有汉堡菜单的响应式导航栏。但是,当我单击汉堡菜单时,我只看到汉堡菜单的动画发生变化(从 3 行变为 2 行,类似于三角形),但我通常在全屏尺寸下看到的导航菜单没有显示。接下来我可以尝试什么?
在这里你应该能够看到行为https://codepen.io/Robmelikn/pen/qBMLzEj(我希望我做对了,这是我第一次使用它)提前感谢您的时间和帮助! !
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>
</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);
});
});
我已经尝试在这里获得帮助,我得到了轻微的改善,但我被建议打开一个新主题汉堡包图标点击不显示菜单
将顶部添加到 css 的这一部分。
.mobile-menu.active {
display: block;
top: 4%;
}