画布外菜单
创建于 2024-12-03 /
50
字体:
[默认]
[大]
[更大]
了解如何创建画布外菜单。
× About Services Clients Contact × About Services Clients Contact × About Services Clients Contact
亲自试一试 »
创建画布外菜单
步骤 1) 添加 HTML:
实例
<div id="mySidenav" class="sidenav"><a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<!-- 使用任意元素打开 sidenav -->
<span onclick="openNav()">open</span>
<!-- 如果您希望侧导航将页面内容推送到右侧,则在此 div 中添加所有页面内容(如果您只希望侧导航位于页面顶部,则不使用) -->
<div id="main">
...
</div>
步骤 2) 添加 CSS:
实例
/* 侧边导航菜单 */.sidenav {
height: 100%; /* 100% 全高 */
width: 0; /* 0 宽度 - 使用 JavaScript 更改 */
position: fixed; /* 原地不动 */
z-index: 1; /* 保持领先 */
top: 0;
left: 0;
background-color: #111; /* 黑色*/
overflow-x: hidden; /* 禁用水平滚动 */
padding-top: 60px; /* 将内容从顶部放置 60px */
transition: 0.5s; /* 0.5 秒过渡效果在侧导航中滑动 */
}
/* 导航菜单链接 */
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
/* 当您将鼠标悬停在导航链接上时,更改它们的颜色 */
.sidenav a:hover {
color: #f1f1f1;
}
/* 关闭按钮的位置和样式(右上角) */
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
/* 样式页面内容 - 如果您想在打开侧导航时将页面内容推送到右侧,请使用此选项 */
#main {
transition: margin-left .5s;
padding: 20px;
}
/* 在高度小于 450 像素的小屏幕上,更改 sidenav 的样式(更少的填充和更小的字体大小)*/
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
步骤 3) 添加 JavaScript:
Off-Canvas Menu
/* 设置侧边导航的宽度为250px,页面内容的左边距为250px */function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
/* 设置侧边导航的宽度为0,页面内容的左边距为0 */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
} 亲自试一试 »
下面的例子也是在侧边导航中滑动,并将页面内容推到右边,只是这次,我们给 body 元素添加了一个不透明度为 40% 的黑色背景色,以"突出显示"侧边导航:
带不透明度的画布外菜单
/* 设置侧边导航的宽度为250px,页面内容的左边距为 250px,并为 body 添加黑色背景色 */function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
/* 设置侧边导航的宽度为0,页面内容的左边距为0,body的背景色为白色 */
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
} 亲自试一试 »
提示: 转到我们的 CSS 导航栏教程,了解有关导航栏的更多信息。
0 人点赞过