我还包括了一个toggle btn,由Font Awesome提供,并将其集成到我的项目中的html头部。
<head>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
</head>
这是导航栏的代码。
<div class="Navbar3">
<div class="Navbar__Link Navbar__Link-brand">
</div>
<div class="Navbar__Link Navbar__Link-toggle">
<i class="fas fa-bars"></i>
</div>
<nav class="Navbar__Items">
<div class="Navbar__Link active">
All
</div>
<div class="Navbar__Link">
<a href="graphic.html">Design</a>
</div>
<div class="Navbar__Link">
<a href="animation.html">Animation</a>
</div>
</nav>
</div>
.Navbar3 {
background-color: #2262AD;
display: flex;
padding: 16px;
font-family: sans-serif;
color: white;
justify-content: center;
}
.Navbar__Link {
padding-right: 25px;
font-weight: bold;
align-self: center;
justify-content: center;
cursor: pointer;
}
.Navbar__Items {
display: flex;
}
.Navbar__Link-toggle {
display: none;
}
@media only screen and (max-width: 768px) {
.Navbar__Items,
.Navbar3 {
flex-direction: column;
}
.Navbar__Items {
display: none;
}
.Navbar__Items--right {
margin-left: 0;
}
.Navbar__ToggleShow {
display: flex;
}
.Navbar__Link-toggle {
align-self: flex-end;
display: initial;
position: absolute;
cursor: pointer;
}
}
.Navbar__Link:hover{
color: black;
}
.active {
color: black;
text-decoration: underline;
}
这里有一些js用于切换
function classToggle() {
const navs = document.querySelectorAll('.Navbar__Items')
navs.forEach(nav => nav.classList.toggle('Navbar__ToggleShow'));
}
document.querySelector('.Navbar__Link-toggle')
.addEventListener('click', classToggle);