0

我想制作一个像这样的画布菜单,但我会将文本推出屏幕而不是裁剪。我想添加一个在我点击菜单后关闭菜单的功能。

我从 w3school 的画布菜单中获得了代码。

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
}
body {
    font-family: "Lato", sans-serif;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
</div>

4

1 回答 1

0

而不是尝试在点击事件上添加/删除 CSS 规则 - 尝试使用添加/删除类的单个函数来实现打开/关闭。

我创建了一个开放类,它具有您在 js 中的样式 - 然后它只是添加/删除类以允许 sidenav 打开/关闭的情况。

这不是我建议的方式 - 但尽可能与您的原始代码保持联系 - 我将整个东西包装在一个带有包装器 id 的 div 中 - 然后在单击该 div 时说(其中包含侧导航和主面板),如果包装器具有开放类 - 然后将其删除(关闭侧导航),如果没有 - 打开它。

您将需要查看要触发打开的内容-我假设您不希望在打开侧中殿的整个疼痛面板上进行任何单击-但这应该使您了解如何在 sidenav 之外单击关闭它。

function toggleNav() {
  var wrapper = document.getElementById("wrapper");
  
  if(wrapper.classList.contains("open")){
    wrapper.classList.remove("open")
  } else {
    wrapper.classList.add("open");
  };
}
body {
    font-family: "Lato", sans-serif;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.open #mySidenav {
  width:250px;  
}

.open #main {
  margin-left:250px;
}


.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover, .offcanvas a:focus{
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
<div id="wrapper" class="" onclick="toggleNav()">
<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span style="font-size:30px;cursor:pointer">&#9776; open</span>
</div>
<div>

于 2017-04-09T11:22:03.593 回答