关闭画布菜单html css

时间:2017-04-09 11:01:09

标签: javascript html css off-canvas-menu

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

我从画布菜单中获取了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>

1 个答案:

答案 0 :(得分:0)

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

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

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

你需要看看你想要触发开口的东西 - 我假设你不希望任何点击整个疼痛面板打开侧面中殿 - 但这应该让你知道如何在外面点击关闭它的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>