|
|
|
|
|
現(xiàn)在的網(wǎng)頁(yè)設(shè)計(jì)多是響應(yīng)式的了,而菜單又是網(wǎng)頁(yè)里用戶交互最多的最醒目的一部分,因此這個(gè)設(shè)計(jì)往往就更重要。本文介紹一個(gè)簡(jiǎn)單卻實(shí)用的響應(yīng)式導(dǎo)航設(shè)計(jì)。
簡(jiǎn)單實(shí)用的響應(yīng)式導(dǎo)航設(shè)計(jì)
實(shí)例簡(jiǎn)介
瀏覽器窗口縮小或放大,當(dāng)?shù)竭_(dá)一個(gè)設(shè)定的臨界寬度,菜單隨之變化,橫向和豎向進(jìn)行切換。
豎向菜單可以打開(kāi)和關(guān)閉,打開(kāi)時(shí)是由左向右快速?gòu)棾觥?/p>
橫向菜單顯示在網(wǎng)頁(yè)右上邊。
代碼介紹
下面是橫豎導(dǎo)航切換的臨界寬度CSS定義,768px。
@media only screen and (max-width: 768px) {
... ...
}
導(dǎo)航打開(kāi)后,按鈕是一個(gè)叉(X);導(dǎo)航關(guān)閉后,按鈕是三橫。
CSS
.hamburger {
display: block;
cursor: pointer;
}
.hamburger.active .bar:nth-child(2) {
opacity: 0;
}
.hamburger.active .bar:nth-child(1) {
-webkit-transform: translateY(8px) rotate(45deg);
transform: translateY(8px) rotate(45deg);
}
.hamburger.active .bar:nth-child(3) {
-webkit-transform: translateY(-8px) rotate(-45deg);
transform: translateY(-8px) rotate(-45deg);
}
HTML
<div class="hamburger">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
當(dāng)點(diǎn)擊菜單項(xiàng)目或關(guān)閉按鈕時(shí),豎向?qū)Ш较?,所以需要JS來(lái)實(shí)現(xiàn)這個(gè)效果。
Javascript
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
const navLink = document.querySelectorAll(".nav-link");
const mobileMenu = () => {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
};
const closeMenu = () => {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
};
hamburger.addEventListener("click", mobileMenu);
navLink.forEach(l => l.addEventListener("click", closeMenu));
完整HTML代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css responsive navbar </title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 62.5%;
font-family: "Roboto", sans-serif;
}
li {
list-style: none;
}
a {
text-decoration: none;
}
span {
color: white;
}
img{
height:40px;
width:40px;
object-fit:cover;
border-radius:50%;
}
.header {
border-bottom: 1px solid #e2e8f0;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 1.5rem;
z-index:999;
}
.bar {
display: block;
width: 25px;
height: 3px;
margin: 5px auto;
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
background-color: #101010;
}
.hamburger {
display: none;
}
.nav-menu {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-item {
margin-left: 5rem;
}
.nav-link {
font-size: 1.6rem;
font-weight: 400;
color: #475569;
}
.nav-link:hover {
color: #482ff7;
}
.nav-logo {
font-size: 2.1rem;
font-weight: 500;
color: #482ff7;
}
@media only screen and (max-width: 768px) {
.nav-menu {
position: fixed;
left: -100%;
top: 5rem;
flex-direction: column;
background-color: #fff;
width: 100%;
border-radius: 10px;
text-align: center;
transition: 0.3s;
box-shadow: 0 10px 27px rgba(0, 0, 0, 0.05);
}
.nav-menu.active {
left: 0;
}
.nav-item {
margin: 2.5rem 0;
}
.hamburger {
display: block;
cursor: pointer;
}
.hamburger.active .bar:nth-child(2) {
opacity: 0;
}
.hamburger.active .bar:nth-child(1) {
-webkit-transform: translateY(8px) rotate(45deg);
transform: translateY(8px) rotate(45deg);
}
.hamburger.active .bar:nth-child(3) {
-webkit-transform: translateY(-8px) rotate(-45deg);
transform: translateY(-8px) rotate(-45deg);
}
}
</style>
</head>
<body>
<header class="header">
<nav class="navbar">
<a href="#" class="nav-logo"><img src="logo.jpg"></img></a>
<ul class="nav-menu">
<li class="nav-item">
<a href="#" class="nav-link">首頁(yè)</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">展示</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">關(guān)于</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">聯(lián)系</a>
</li>
</ul>
<div class="hamburger">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
</nav>
</header>
<script id="rendered-js" >
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-menu");
const navLink = document.querySelectorAll(".nav-link");
const mobileMenu = () => {
hamburger.classList.toggle("active");
navMenu.classList.toggle("active");
};
const closeMenu = () => {
hamburger.classList.remove("active");
navMenu.classList.remove("active");
};
hamburger.addEventListener("click", mobileMenu);
navLink.forEach(l => l.addEventListener("click", closeMenu));
</script>
</body>
</html>
總結(jié)
本文介紹了一個(gè)簡(jiǎn)單實(shí)用的響應(yīng)式導(dǎo)航設(shè)計(jì)。響應(yīng)式網(wǎng)頁(yè)你學(xué)會(huì)了嗎?初學(xué)者可以看看這篇文章《一天完成把PC網(wǎng)站改為自適應(yīng)!原來(lái)這么簡(jiǎn)單!》。