|
|
|
|
|
本文介紹一個(gè)可縮進(jìn)的側(cè)欄導(dǎo)航菜單,使用純CSS3實(shí)現(xiàn)。
CSS3實(shí)現(xiàn)的可縮進(jìn)的側(cè)欄導(dǎo)航菜單
下面是側(cè)欄導(dǎo)航的HTML代碼,主要是使用UL-LI結(jié)構(gòu)。注意到還用了一個(gè)SVG代碼,這個(gè)其實(shí)導(dǎo)航上面最頂部那個(gè)logo圖標(biāo)的SVG實(shí)現(xiàn)代碼,如果我們不用那個(gè)logo圖標(biāo),可以不要這個(gè)段SVG代碼。
<nav id="navbar">
<ul class="navbar-items flexbox-col">
<li class="navbar-logo flexbox-left">
<a class="navbar-item-inner flexbox">
<svg xmlns="http://www.w3.org/2000/svg" id="Layer_1" data-name="Layer 1" viewBox="0 0 1438.88 1819.54">
<polygon points="925.79 318.48 830.56 0 183.51 1384.12 510.41 1178.46 925.79 318.48"/>
<polygon points="1438.88 1663.28 1126.35 948.08 111.98 1586.26 0 1819.54 1020.91 1250.57 1123.78 1471.02 783.64 1663.28 1438.88 1663.28"/>
</svg>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="search-outline"></ion-icon>
</div>
<span class="link-text">搜索</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="home-outline"></ion-icon>
</div>
<span class="link-text">主頁(yè)</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="folder-open-outline"></ion-icon>
</div>
<span class="link-text">工程</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="pie-chart-outline"></ion-icon>
</div>
<span class="link-text">面版</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="people-outline"></ion-icon>
</div>
<span class="link-text">團(tuán)隊(duì)</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="chatbubbles-outline"></ion-icon>
</div>
<span class="link-text">支持</span>
</a>
</li>
<li class="navbar-item flexbox-left">
<a class="navbar-item-inner flexbox-left">
<div class="navbar-item-inner-icon-wrapper flexbox">
<ion-icon name="settings-outline"></ion-icon>
</div>
<span class="link-text">設(shè)置</span>
</a>
</li>
</ul>
</nav>
我們還注意到,在HTML代碼里,后面有兩個(gè)js文件,這是導(dǎo)航菜單每個(gè)項(xiàng)目前面的icon圖標(biāo)庫(kù)文件,如果沒(méi)有這兩個(gè)js文件,則導(dǎo)航每個(gè)項(xiàng)目前面就沒(méi)有小圖標(biāo),那樣會(huì)不太好看。
<script src='https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js'></script>
<script src='https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js'></script>
下面僅貼出導(dǎo)航的CSS代碼。
#navbar {
top: 0;
padding: 0;
width: 5em;
height: 100vh;
position: fixed;
background-color: hsl(var(--background-secondary));
transition: width .35s cubic-bezier(var(--transition-main), 1);
overflow-y: auto;
overflow-x: hidden;
}
#navbar:hover {
width: 16em;
}
#navbar::-webkit-scrollbar-track {
background-color: hsl(var(--background-secondary));
}
#navbar::-webkit-scrollbar {
width: 8px;
background-color: hsl(var(--background-secondary));
}
#navbar::-webkit-scrollbar-thumb {
background-color: hsl(var(--primary));
}
.navbar-items {
margin: 0;
padding: 0;
list-style-type: none;
}
/* Navbar Logo */
.navbar-logo {
margin: 0 0 2em 0;
width: 100%;
height: 5em;
background: hsl(var(--background-secondary-dark));
}
.navbar-logo > .navbar-item-inner {
width: calc(5rem - 8px);
}
.navbar-logo > .navbar-item-inner:hover {
background-color: transparent;
}
.navbar-logo > .navbar-item-inner > svg {
height: 2em;
fill: hsl(var(--white));
}
/* Navbar Items */
.navbar-item {
padding: 0 .5em;
width: 100%;
cursor: pointer;
}
.navbar-item-inner {
padding: 1em 0;
width: 100%;
position: relative;
color: hsl(var(--quite-gray));
border-radius: .25em;
text-decoration: none;
transition: all .2s cubic-bezier(var(--transition-main), 1);
}
.navbar-item-inner:hover {
color: hsl(var(--white));
background: hsl(var(--background-secondary-light));
box-shadow: 0 17px 30px -10px hsla(var(--black), .25);
}
.navbar-item-inner-icon-wrapper {
width: calc(5rem - 1em - 8px);
position: relative;
}
.navbar-item-inner-icon-wrapper ion-icon {
position: absolute;
font-size: calc(var(--navbar-buttons) - 1rem);
}
.link-text {
margin: 0;
width: 0;
text-overflow: ellipsis;
white-space: nowrap;
transition: all .35s cubic-bezier(var(--transition-main), 1);
overflow: hidden;
opacity: 0;
}
#navbar:hover .link-text {
width: calc(100% - calc(5rem - 8px));
opacity: 1;
}
這個(gè)導(dǎo)航加入了不少的鼠標(biāo)懸停效果。
還有,這是一個(gè)響應(yīng)式的側(cè)欄導(dǎo)航菜單,可縮進(jìn)效果是它一大特色,而它是純CSS3實(shí)現(xiàn)的。