W3.CSS 侧边栏
W3.CSS 垂直导航栏
使用侧面导航,您有多种选择:
- 始终在页面内容的左侧显示导航窗格
- 使用可折叠的 "fully automatic" 响应式侧边导航
- 打开页面内容左侧的导航窗格
- 打开所有页面内容的导航窗格
- 打开导航栏时向右滑动页面内容
- 在右侧而不是左侧显示导航窗格
始终显示侧边栏
实例
<div class="w3-sidebar w3-bar-block" style="width:25%">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
<div style="margin-left:25%">
... page content ...
</div>
亲自试一试 »
在部分内容上打开侧边栏导航
实例
function w3_open() {
document.getElementById("mySidebar").style.display = "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
}
亲自试一试 »
打开侧边栏导航内容
实例
function w3_open() {
document.getElementById("mySidebar").style.width
= "100%";
document.getElementById("mySidebar").style.display
= "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
}
亲自试一试 »
可折叠的响应式侧边导航
实例
<div class="w3-sidebar
w3-bar-block w3-collapse w3-card" style="width:200px;" id="mySidebar">
<button class="w3-bar-item
w3-button w3-hide-large"
onclick="w3_close()">Close ×</button>
<a
href="#" class="w3-bar-item w3-button">Link 1</a>
<a
href="#" class="w3-bar-item w3-button">Link 2</a>
<a
href="#" class="w3-bar-item w3-button">Link 3</a>
</div>
<div class="w3-main" style="margin-left:200px">
<div class="w3-teal">
<button class="w3-button w3-teal
w3-xlarge" onclick="w3_open()">☰</button>
<div
class="w3-container">
<h1>My Page</h1>
</div>
</div>
</div>
<script>
function w3_open()
{
document.getElementById("mySidebar").style.display
= "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
}
</script>
亲自试一试 »
将页面内容向右滑动
实例
function w3_open() {
document.getElementById("main").style.marginLeft
= "25%";
document.getElementById("mySidebar").style.width
= "25%";
document.getElementById("mySidebar").style.display
= "block";
document.getElementById("openNav").style.display
= 'none';
}
function w3_close() {
document.getElementById("main").style.marginLeft
= "0%";
document.getElementById("mySidebar").style.display
= "none";
document.getElementById("openNav").style.display
= "inline-block";
}
亲自试一试 »
右侧侧边导航
实例
<div class="w3-sidebar
w3-bar-block" style="width:25%;right:0">
<h5 class="w3-bar-item">Menu</h5>
<a href="#" class="w3-bar-item
w3-button">Link 1</a>
<a href="#"
class="w3-bar-item w3-button">Link 2</a>
<a href="#"
class="w3-bar-item w3-button">Link 3</a>
</div>
<div style="margin-right:25%">
... page content ...
</div>
亲自试一试 »
右侧可折叠导航
实例
<div class="w3-sidebar w3-bar-block w3-collapse" style="width:200px;right:0"
id="mySidebar">
<button class="w3-bar-item w3-button w3-hide-large"
onclick="w3_close()">Close
×</button>
<a href="#" class="w3-bar-item w3-button">Link
1</a>
<a href="#"
class="w3-bar-item w3-button">Link 2</a>
<a href="#" class="w3-bar-item w3-button">Link
3</a>
</div>
<div class="w3-main" style="margin-right:200px">
<div class="w3-teal">
<button class="w3-button w3-teal w3-xlarge
w3-right w3-hide-large" onclick="w3_open()">☰</button>
<div
class="w3-container">
<h2>My Page</h2>
</div>
</div>
</div>
<script>
function w3_open()
{
document.getElementById("mySidebar").style.display
= "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
}
</script>
亲自试一试 »
左侧和右侧导航
侧边导航样式
将 w3-color 类添加到 w3-sidebar 以更改背景颜色。如果您想要一个活动/当前链接,让用户知道他/她在哪个页面上,请将 w3-color 类添加到其中一个链接:
带边框的侧边导航
使用 w3-border 类在侧边导航周围添加边框:
将 w3-border-bottom 类添加到链接以创建链接分隔线:
实例
<div class="w3-sidebar w3-bar-block">
<a href="#" class="w3-bar-item w3-button w3-border-bottom">Link 1</a>
<a href="#"
class="w3-bar-item w3-button w3-border-bottom">Link 2</a>
<a href="#"
class="w3-bar-item w3-button">Link 3</a>
</div>
亲自试一试 »
使用 w3-card 类将侧边导航显示为卡片:
悬停链接
当您将鼠标悬停在条块内的链接上时,背景颜色将变为灰色。
如果你想在悬停时使用不同的背景颜色,请使用任何 w3-hover-color 类:
实例
<div class="w3-sidebar w3-bar-block">
<a href="#" class="w3-bar-item w3-button w3-hover-black">Link 1</a>
<a href="#"
class="w3-bar-item w3-button w3-hover-green">Link 2</a>
<a href="#"
class="w3-bar-item w3-button w3-hover-blue">Link 3</a>
</div>
亲自试一试 »
您可以使用 w3-hover-none 类关闭默认悬停效果。 当您只想在悬停时突出显示文本颜色(而不是背景颜色)时,通常会使用这种方法:
实例
<div class="w3-sidebar w3-bar-block">
<a href="#" class="w3-bar-item w3-button w3-hover-none w3-hover-text-grey">Link
1</a>
<a href="#"
class="w3-bar-item w3-button w3-hover-none w3-hover-text-green">Link
2</a>
<a href="#"
class="w3-bar-item w3-button w3-hover-none w3-hover-text-teal">Link
3</a>
</div>
亲自试一试 »
侧边导航尺寸
增加字体大小(w3-large 等):
增加填充(w3-padding 等):
实例
<div class="w3-sidebar w3-bar-block w3-large">
<a href="#" class="w3-bar-item
w3-button">Link</a>
<a href="#" class="w3-bar-item w3-button">Link</a>
<a href="#" class="w3-bar-item w3-button">Link</a>
</div>
亲自试一试 »
带图标的侧边导航
实例
<div class="w3-sidebar w3-bar-block w3-black" style="width:70px">
<a href="#"
class="w3-bar-item w3-button"><i
class="fa fa-home"></i></a>
<a href="#"
class="w3-bar-item w3-button"><i class="fa
fa-search"></i></a>
<a href="#" class="w3-bar-item
w3-button"><i class="fa fa-envelope"></i></a>
<a href="#"
class="w3-bar-item w3-button"><i class="fa fa-globe"></i></a>
</div>
亲自试一试 »
带下拉菜单的侧边栏
实例
<div class="w3-sidebar w3-bar-block">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<div
class="w3-dropdown-hover">
<button
class="w3-button">Dropdown
<i class="fa fa-caret-down"></i></button>
<div
class="w3-dropdown-content w3-bar-block">
<a href="#"
class="w3-bar-item w3-button">Link</a>
<a href="#"
class="w3-bar-item w3-button">Link</a>
</div>
</div>
<a href="#"
class="w3-bar-item w3-button">Link
3</a>
</div>
亲自试一试 »
提示: 当下拉菜单处于"打开"状态时,下拉链接的背景色为灰色,表示它处于活动状态。要覆盖它,请将 w3-hover-color 类添加到 "dropdown" <div> 和 <a>.
带手风琴的侧边栏
实例
<div class="w3-sidebar w3-light-grey w3-card" style="width:200px">
<a href="#" class="w3-bar-item w3-button">Link 1</a>
<button class="w3-button w3-block w3-left-align" onclick="myAccFunc()">Accordion</button>
<div
id="demoAcc" class="w3-bar-block w3-hide w3-white w3-card-4">
<a href="#" class="w3-bar-item w3-button">Link</a>
<a href="#"
class="w3-bar-item w3-button">Link</a>
</div>
<div class="w3-dropdown-click">
<button class="w3-button" onclick="myDropFunc()">Dropdown</button>
<div
id="demoDrop" class="w3-dropdown-content w3-bar-block w3-white w3-card-4">
<a href="#" class="w3-bar-item w3-button">Link</a>
<a href="#"
class="w3-bar-item w3-button">Link</a>
</div>
</div>
<a href="#" class="w3-bar-item
w3-button">Link 2</a>
<a href="#"
class="w3-bar-item w3-button">Link
3</a>
</div>
动画侧边栏
使用任何 w3-animate-类 淡入淡出、缩放或滑入侧边导航:
侧边栏覆盖
w3-overlay 类可用于在打开侧边栏时创建叠加效果。w3-overlay 类为 "page content" 添加了一个 50% 不透明度的黑色背景 - 这种效果将"突出"侧边导航。
实例
<!-- Sidebar -->
<div class="w3-sidebar w3-bar-block w3-white w3-animate-left" style="display:none;z-index:4" id="mySidebar">
<button
class="w3-bar-item w3-button"
onclick="w3_close()">Close</button>
<a href="#"
class="w3-bar-item w3-button">Link
1</a>
<a href="#" class="w3-bar-item w3-button">Link 2</a>
<a href="#"
class="w3-bar-item w3-button">Link 3</a>
</div>
<!-- Overlay -->
<div class="w3-overlay" onclick="w3_close()" style="cursor:pointer"></div>
<!-- Page content -->
<button class="w3-button w3-xxlarge"
onclick="w3_open()">☰</button>
<div class="w3-container">
<h1>Sidebar Overlay</h1>
<p>Click on the "hamburger" icon to slide
in the side navigation.</p>
</div>
<!-- JS to
open and close sidebar with overlay effect -->
<script>
function
w3_open() {
document.getElementById("mySidebar").style.display
= "block";
document.getElementById("myOverlay").style.display
= "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
document.getElementById("myOverlay").style.display = "none";
}
</script>
亲自试一试 »
侧边栏内容
在侧面导航中添加您喜欢的任何内容:
实例
<div class="w3-sidebar w3-bar-block w3-light-grey" style="width:50%">
<div
class="w3-container w3-dark-grey">
<h4>Menu</h4>
</div>
<img src="img_snowtops.jpg">
<a href="#"
class="w3-bar-item w3-button w3-red">Home</a>
<a href="#" class="w3-bar-item w3-button">Projects
<span
class="w3-tag w3-red w3-round w3-right">8</span>
</a>
<a href="#" class="w3-bar-item w3-button">About</a>
<a href="#"
class="w3-bar-item w3-button">Contact</a>
<div class="w3-panel w3-blue-grey
w3-display-container">
<span class="w3-button w3-display-topright">X</span>
<p>Lorem
ipsum box...</p>
</div>
</div>
亲自试一试 »