通过单击外部关闭菜单

时间:2019-11-19 14:50:44

标签: javascript html css

我有一个小菜单,我想在单击页面上的任何位置时自动将其关闭。我知道这可能只是添加一行代码的问题,但是到目前为止我尝试过的所有方法都无法正常工作...

现在它只是js函数:

  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}

编辑:对不起,对不起,我不知道我可以编辑第一篇文章...这是其余的代码:

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

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
body {}

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

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  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;
}

@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>
  <h2>Position #1</a>
  </h2>
  <h2>Position #2</a>
  </h2>
  <h2>Position #3</a>
  </h2>
  <h2>Position #4</a>
  </h2>
  <h2>Position #5</a>
  </h2>
  <h2>Position #6</a>
  </h2>
  <h2>Position #7</a>
  </h2>
  <h2>Position #8</a>
  </h2>
  <h2>Position #9</a>
  </h2>
  <h2>Position #10</a>
  </h2>
</div>
<span style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
}

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

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  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;
}

@media screen and (max-height 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
</head>
<body>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<h2>Position #1</a></h2>
<h2>Position #2</a></h2>
<h2>Position #3</a></h2>
<h2>Position #4</a></h2>
<h2>Position #5</a></h2>
<h2>Position #6</a></h2>
<h2>Position #7</a></h2>
<h2>Position #8</a></h2>
<h2>Position #9</a></h2>
<h2>Position #10</a></h2>
</div>

<span style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>

<script>
function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:3)

要隐藏元素,您应该使用style.display = "none"

function closeNav() {
  document.getElementById("mySidenav").style.display = "none";
}

要再次显示,请将显示恢复原状。很多时候是block

function openNav() {
  document.getElementById("mySidenav").style.display = "block";
}

在您的情况下,由于您没有提供足够的代码,因此我假设您还需要知道在何处调用closeNav函数。您可以在文档上执行此操作:

document.onclick = function() {
    closeNav();
}

这是基于您提供的代码的解决方案:

document.onclick = function(evt) {
  if (!event.target.matches("#menu")) {
    closeNav();
  }
}

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

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
}
body {}

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

.sidenav a {
  padding: 8px 8px 8px;
  text-decoration: none;
  font-size: 15px;
  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;
}

@media screen and (max-height 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <h2>Position #1</h2>
    <h2>Position #2</h2>
    <h2>Position #3</h2>
    <h2>Position #4</h2>
    <h2>Position #5</h2>
    <h2>Position #6</h2>
    <h2>Position #7</h2>
    <h2>Position #8</h2>
    <h2>Position #9</h2>
    <h2>Position #10</h2>
  </div>

  <span id="menu" style="font-size:20px;cursor:pointer" onclick="openNav()">&#9776;Menu</span>


</body>

</html>

答案 1 :(得分:0)

您可以尝试

function closeNav() {
  document.getElementById("mySidenav").style.display= "none";
}