Wie man erstellt: Off-Canvas-Menü

Lernen Sie, wie man ein Off-Canvas-Menü erstellt.



亲自试一试

Erstellen Sie ein Off-Canvas-Menü

Erste Schritte - HTML hinzufügen:

<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>
<!-- 使用任意元素打开侧边导航 -->
<span onclick="openNav()">open</span>
<!-- 如果您希望侧边导航将页面内容推向右侧,请将所有页面内容添加到此 div 内(如果只想让侧边导航停留在页面顶部,则无需使用此设置) -->
<div id="main">
  ...
</div>

第二步 - 添加 CSS:

/* 侧边导航菜单 */
.sidenav {
  height: 100%; /* 100% 全高 */
  width: 0; /* 0 width - 通过 JavaScript 更改此设置 */
  position: fixed; /* 保持原位 */
  z-index: 1; /* 保持在顶部 */
  top: 0;
  left: 0;
  background-color: #111; /* 黑色 */
  overflow-x: hidden; /* 禁用水平滚动 */
  padding-top: 60px; /* 内容距离顶部 60 像素 */
  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;
}
/* Set the style of the page content - if you want to push the page content to the right when the side navigation is opened, use this option */
#main {
  transition: margin-left .5s;
  padding: 20px;
}
/* Change the style of the side navigation bar on small screens with a screen height less than 450 pixels (reduce padding and font size) */
@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}

Step 3 - Add JavaScript:

Menu outside the canvas

/* Set the width of the side navigation to 250px, and the left margin of the page content to 250px */
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}
/* Set the width of the side navigation to 0, and the left margin of the page content to 0 */
function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}

亲自试一试

The following example also slides the side navigation bar into place and pushes the page content to the right. However, this time we add a semi-transparent (40% opacity) black background color to the body element to "highlight" the side navigation bar:

Menu outside the canvas with transparency

/* Set the width of the side navigation to 250px, the left outer margin of the page content to 250px, and add a black background color to the 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)";
}
/* Set the width of the side navigation to 0, the left margin of the page content to 0, and the background color of the body to white */
function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
  document.body.style.backgroundColor = "white";
}

亲自试一试

相关页面

教程:CSS 导航栏