在本教程中,您将使用JavaScript和CSS创建一个可扩展的侧面导航菜单。 最终产品将显示如下:
标记
首先,让我们为侧边菜单添加一些标记:
<div id="sideNavigation" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Features</a>
<a href="#">Contact Us</a>
</div>
<nav class="topnav">
<a href="#" onclick="openNav()">
<svg width="30" height="30" id="icoOpen">
<path d="M0,5 30,5" stroke="#000" stroke-width="5"/>
<path d="M0,14 30,14" stroke="#000" stroke-width="5"/>
<path d="M0,23 30,23" stroke="#000" stroke-width="5"/>
</svg>
</a>
</nav>
<div id="main">
<!-- Add all your websites page content here -->
</div>
在这里,您可以看到我们用sidenav
类创建了一个侧面菜单div。 接下来,我们通过<nav>
标签添加了实际的顶部导航条,并且在侧面菜单图标中使用了SVG。
图标和关闭按钮的onclick
属性将触发一些JavaScript,接下来我们将添加它们。
请记住,将您网站的所有内容放入div id="main"
容器中,以使其向右滑动。
JavaScript
接下来,让我们添加JavaScript来制作openNav
和closeNav
函数。
<script>
function openNav() {
document.getElementById("sideNavigation").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
document.getElementById("sideNavigation").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
}
</script>
CSS
最后,我们需要为侧边菜单和链接添加一些CSS样式的页面:
/* The side navigation menu */
.sidenav {
height: 100%; /* 100% Full-height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 0;
left: 0;
background-color: #111; /* Black*/
overflow-x: hidden; /* Disable horizontal scroll */
padding-top: 60px; /* Place content 60px from the top */
transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}
/* The navigation menu links */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover, .offcanvas a:focus{
color: #f1f1f1;
}
/* Position and style the close button (top right corner) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
transition: margin-left .5s;
padding: 20px;
overflow:hidden;
width:100%;
}
body {
overflow-x: hidden;
}
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
}
/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
a svg{
transition:all .5s ease;
&:hover{
#transform:rotate(180deg);
}
}
#ico{
display: none;
}
.menu{
background: #000;
display: none;
padding: 5px;
width: 320px;
@include border-radius(5px);
#transition: all 0.5s ease;
a{
display: block;
color: #fff;
text-align: center;
padding: 10px 2px;
margin: 3px 0;
text-decoration: none;
background: #444;
&:nth-child(1){
margin-top: 0;
@include border-radius(3px 3px 0 0 );
}
&:nth-child(5){
margin-bottom: 0;
@include border-radius(0 0 3px 3px);
}
&:hover{
background: #555;
}
}
}
注意 :必须使用body {overflow-x: hidden;}
,以确保与现有CSS一起使用时不会出现水平滚动。
现在,您可以查看菜单,然后在浏览器中尝试一下。
使用jQuery
如果要使用jQuery创建侧边菜单JavaScript,则可以通过用以下部分替换我之前提供JavaScript来做到这一点:
$('.topnav a').click(function(){
$('#sideNavigation').style.width = "250px";
$("#main").style.marginLeft = "250px";
});
$('.closebtn').click(function(){
$('#sideNavigation').style.width = "0";
$("#main").style.marginLeft = "0";
});
卸下幻灯片
要使菜单不显示任何幻灯片动画,只需更改CSS属性transition
,如下面的缩写形式所示:
.sidenav {
transition: 0s;
}
#main {
transition: margin-left 0s;
}
这将使更改立即显示,因为在transition
没有指定延迟。 我们使用的默认值为0.5s
。
结论
创建侧边菜单仅需要几行代码,并且不需要使用大量资源。 此外,如果jQuery已经在页面上执行其他任务,则可以用较少的代码行来完成工作并进一步自定义。
使代码响应于不同的设备屏幕分辨率而仅仅是通过添加针对特定情况的媒体查询来修改CSS的情况。
为了更进一步,您可能希望使用CSS框架(如Bootstrap或Bulma)来设置菜单样式。