我不喜欢直接通过元素名称来设置样式,因此我添加了类来进行样式控制。此外,我还设置了一个针对 (min-width: 768px)
的媒体查询,这样一来,在大屏幕设备上,汉堡菜单将会被隐藏,元素始终保持显示状态。我还基于默认字体大小 font-size:16px
(等于 1em
,这是大多数浏览器的默认值)将单位转换为了 em
。
现在重点是,我随后使用了带有值的数据属性,这样我就可以在 CSS 中利用这个属性来控制菜单显示/隐藏,而不是直接通过 JavaScript 修改样式,因为随着屏幕尺寸的变化,直接修改样式的做法较为复杂。这种改进方法简化了实现方式,仅使用 CSS 就能保持根据汉堡菜单点击来切换菜单的显示与隐藏功能。
最后一个小修复是在媒体查询结尾处加上了终止的 }
符号,尽管这可能只是个笔误。
//const nameOfElement = document.querySelector(".nav-items");
//var dd = "closed";
var hamburgerIcon = document.querySelector(".hamburger").querySelector(".hamburger-icon");
hamburgerIcon.addEventListener("click", (event) => {
const nav = document.querySelector(".nav-items");
var navStyle = nav.dataset.menustyle;
nav.dataset.menustyle = navStyle == "closed" ? "opened" : "closed";
});
* {
font-size: 16px;
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0;
}
.wrapper {
background: white;
padding-top: 0.75em;
padding-bottom: 0.75em;
box-shadow: 0em 0.125em 0.75em 0em rgba(0, 0, 0, 0.02);
}
nav {
display: flex;
justify-content: space-around;
padding-left: 4em;
padding-right: 4em;
}
.logo {
display: flex;
justify-content: center;
align-items: center;
}
.nav-items {
display: flex;
justify-content: space-around;
gap: 2em;
}
.nav-items[data-menustyle="closed"] {
display: none;
}
.nav-items[data-menustyle="open"] {}
.nav-block {
list-style-type: none;
display: flex;
gap: 2em;
}
.nav-item {
padding-left: 0.5em;
padding-right: 0.5em;
}
.nav-link {
text-decoration: none;
font-family: 'Noto Sans', sans-serif;
font-weight: 500;
font-size: 0.875em;
color: gray;
border: 1px solid #00FF00;
}
.nav-link:hover {
color: black;
}
.button {
display: flex;
justify-content: center;
align-items: center;
}
.in-touch {
width: 9.375em height: 2.625em;
padding: 0.625em 1.25em;
background-color: #A900E4;
color: white;
border-radius: 0.25em;
font-family: 'Noto Sans', sans-serif;
font-weight: 600;
font-size: 0.875em;
}
@media screen and (max-width: 768px) {
nav {
flex-direction: column;
align-content: center;
}
.nav-items {
flex-direction: column;
gap: 8px;
}
.logo {
margin-top: 1em;
}
.nav-block {
flex-direction: column;
justify-content: center;
}
.logo {
display: flex;
justify-content: space-between;
}
.nav-item {
text-align: center;
}
.in-touch-container {
margin-bottom: 2em;
}
.nav-items[data-menustyle="closed"] {
display: none;
}
}
@media screen and (min-width: 768px) {
.hamburger {
display: none;
}
.nav-items[data-menustyle="closed"] {
display: flex;
}
}
<div class="wrapper">
<nav>
<div class="logo">
<h3>Hello</h3>
<div class="hamburger">
<i class="fa-solid fa-bars hamburger-icon"></i>
</div>
</div>
<div class="nav-items" data-menustyle="closed">
<ul class="nav-block">
<li class="nav-item"><a class="nav-link" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">About Me</a></li>
<li class="nav-item"><a class="nav-link" href="#">My Works</a></li>
</ul>
<div class="nav-item in-touch-container">
<button class="in-touch">Get in touch</button>
</div>
</div>
</nav>
</div>
<script src="https://kit.fontawesome.com/dd8784c5d5.js" crossorigin="anonymous"></script>