单击侧边栏菜单外部以及单击菜单项时关闭侧边栏菜单

Close sidebar menu when click outside of it and when click on menu item

目标是在我点击侧边栏菜单外部或点击其中一个菜单项时关闭侧边栏菜单。我在 Javascript 中创建了两个工作函数来打开和关闭菜单,单击切换:

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

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

function clickOutsite() {
TO-DO
}

function clickOnItemAndClose() {
TO-DO
}


</script>

<div class="header"></div>
<input type="checkbox" class="openSideMenu" id="openSideMenu">
<label for="openSideMenu" class="sideIconToggle" onclick="openNav()">
<div class="spinner diagonal part-1"></div>
<div class="spinner horizontal"></div>
<div class="spinner diagonal part-2"></div>
</label>
  <div id="sideMenu">
    <ul class="sideMenuInner">
      <li class="active"><a href="#" data-toggle="tab">Item1</a></li>
      <li><a href="#" data-toggle="tab">Item2</a></li>
      <li><a href="#" data-toggle="tab">Item3</a></li>
    </ul>
  </div>

我试图创建一个覆盖层并创建一个与其连接的函数,但结果无效。

你能帮帮我吗?提前致谢

编辑:这里有一个演示 https://codepen.io/Clara83/pen/PoPVMgN

hide/show 的整个动画是基于 CSS 所以你只需要切换 checked 属性 复选框 <input type="checkbox" class="openSideMenu" id="openSideMenu">

要解决检测外部点击的问题,您可以将侧边栏包裹在 div 中,然后使用 contains 属性 从被点击的元素发出的事件对象

function hideSidebar() {
  document.getElementById('openSideMenu').checked = false;
}

var sideIconToggle = document.getElementById('sidebarContainer');

document.addEventListener('click', function(event) {
  if (!sidebarContainer.contains(event.target))
    hideSidebar();
});
html,
body {
  overflow-x: hidden;
  height: 100%;
}

body {
  background: #fff;
  padding: 0;
  margin: 0;
  font-family: 'Varela Round', sans-serif;
}

.header {
  display: block;
  margin: 0 auto;
  width: 100%;
  max-width: 100%;
  box-shadow: none;
  background-color: black;
  position: fixed;
  height: 60px!important;
  overflow: hidden;
  z-index: 10;
}

.main {
  margin: 0 auto;
  display: block;
  height: 100%;
  margin-top: 60px;
}

.mainInner {
  display: table;
  height: 100%;
  width: 100%;
  text-align: center;
}

.mainInner div {
  display: table-cell;
  vertical-align: middle;
  font-size: 3em;
  font-weight: bold;
  letter-spacing: 1.25px;
}

#sideMenu {
  height: 100%;
  position: fixed;
  left: 0;
  width: 250px;
  margin-top: 60px;
  transform: translateX(-250px);
  transition: transform 250ms ease-in-out;
  background: grey;
  z-index: 1;
}

.sideMenuInner {
  margin: 0;
  padding: 0;
  border-top: 1px solid black;
}

.sideMenuInner li {
  list-style: none;
  color: #fff;
  text-transform: uppercase;
  font-weight: bold;
  padding: 20px;
  cursor: pointer;
  border-bottom: 1px solid black;
}

.sideMenuInner li span {
  display: block;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.50);
}

.sideMenuInner li a {
  color: #fff;
  text-transform: uppercase;
  font-weight: bold;
  cursor: pointer;
  text-decoration: none;
}

input[type="checkbox"]:checked~#sideMenu {
  transform: translateX(0);
}

input[type=checkbox] {
  transition: all 0.3s;
  box-sizing: border-box;
  display: none;
}

.sideIconToggle {
  transition: all 0.3s;
  box-sizing: border-box;
  cursor: pointer;
  position: absolute;
  z-index: 99;
  height: 100%;
  width: 100%;
  top: 22px;
  left: 15px;
  height: 22px;
  width: 22px;
}

.spinner {
  transition: all 0.3s;
  box-sizing: border-box;
  position: absolute;
  height: 3px;
  width: 100%;
  background-color: #fff;
}

.horizontal {
  transition: all 0.3s;
  box-sizing: border-box;
  position: relative;
  float: left;
  margin-top: 3px;
}

.diagonal.part-1 {
  position: relative;
  transition: all 0.3s;
  box-sizing: border-box;
  float: left;
}

.diagonal.part-2 {
  transition: all 0.3s;
  box-sizing: border-box;
  position: relative;
  float: left;
  margin-top: 3px;
}

input[type=checkbox]:checked~.sideIconToggle>.horizontal {
  transition: all 0.3s;
  box-sizing: border-box;
  opacity: 0;
}

input[type=checkbox]:checked~.sideIconToggle>.diagonal.part-1 {
  transition: all 0.3s;
  box-sizing: border-box;
  transform: rotate(135deg);
  margin-top: 8px;
}

input[type=checkbox]:checked~.sideIconToggle>.diagonal.part-2 {
  transition: all 0.3s;
  box-sizing: border-box;
  transform: rotate(-135deg);
  margin-top: -9px;
}

#sidenav-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 0;
  height: 100%;
  background: rgba(0, 0, 0, .1);
  cursor: pointer;
  z-index: 1;
}
<div class="header"></div>

<div id="sidebarContainer">
  <input type="checkbox" class="openSideMenu" id="openSideMenu">

  <label for="openSideMenu" class="sideIconToggle">
    <div class="spinner diagonal part-1"></div>
    <div class="spinner horizontal"></div>
    <div class="spinner diagonal part-2"></div>
  </label>

  <div id="sideMenu">
    <ul class="sideMenuInner">
      <li class="active" onclick="hideSidebar()"><a href="#" data-toggle="tab">Item1</a></li>
      <li onclick="hideSidebar()"><a href="#" data-toggle="tab">Item2</a></li>
      <li onclick="hideSidebar()"><a href="#" data-toggle="tab">Item3</a></li>
    </ul>
  </div>
</div>