Close the sidebar menu when you click it or click on a menu item


Clara 83:

The goal is to close the sidebar menu when I click on the sidebar menu or when I click on one of the menu items. I've created two working functions in Javascript to open and close the menu, click toggle:

<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>

I tried to create an overlay and create a function connected to it, but the result is not valid.

Could you please help me? Thanks in advance

Edit: Here's a demo https://codepen.io/Clara83/pen/PoPVMgN

Viney:

The entire animation of hiding/showing is CSS based, so you only need to toggle the checked property of the checkbox<input type="checkbox" class="openSideMenu" id="openSideMenu">

To solve the problem of detecting external clicks, you can wrap the sidebar in a div and then use the contains property of the event object emitted from the element that was clicked

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>

Related


How to close the hamburger menu when you click on a link?

Dima I'm making a hamburger menu, if I can see it works, but when I click the link it has to close, so how do you close the hamburger menu when you click the link? #menu__toggle { opacity: 0; } #menu__toggle:checked~.menu__btn>span { transform: rotate(45d

How to close the hamburger menu when you click on a link?

Dima I'm making a hamburger menu, if I can see it works, but when I click the link it has to close, so how do you close the hamburger menu when you click the link? #menu__toggle { opacity: 0; } #menu__toggle:checked~.menu__btn>span { transform: rotate(45d

How to close the hamburger menu when you click on a link?

Dima I'm making a hamburger menu, if I can see it works, but when I click the link it has to close, so how do you close the hamburger menu when you click the link? #menu__toggle { opacity: 0; } #menu__toggle:checked~.menu__btn>span { transform: rotate(45d

How to close the hamburger menu when you click on a link?

Dima I'm making a hamburger menu, if I can see it works, but when I click the link it has to close, so how do you close the hamburger menu when you click the link? #menu__toggle { opacity: 0; } #menu__toggle:checked~.menu__btn>span { transform: rotate(45d

How to close the hamburger menu when you click on a link?

Dima I'm making a hamburger menu, if I can see it works, but when I click the link it has to close, so how do you close the hamburger menu when you click the link? #menu__toggle { opacity: 0; } #menu__toggle:checked~.menu__btn>span { transform: rotate(45d

Click on X to close the menu

sambaileyy : I struggled to get the X, removed the active state class's menu, and returned to the "hidden" state. Where am I going wrong in JS? When the menu button is clicked the menu appears, but when the X is clicked nothing happens. How can I restore it to

Click on X to close the menu

sambaileyy : I struggled to get the X, removed the active state class's menu, and returned to the "hidden" state. Where am I going wrong in JS? When the menu button is clicked the menu appears, but when the X is clicked nothing happens. How can I restore it to

Click to close the menu

Dennis Marceau I want to close the menu by clicking a link or clicking outside the menu. To keep things nice and light, I don't want to use jQuery. what should I do? <nav class="nav"> <div class="nav__left"> <h3><a href="#home">DenisMasot</a></h3> </di

Click on X to close the menu

sambaileyy : I struggled to get the X, removed the active state class's menu, and returned to the "hidden" state. Where am I going wrong in JS? When the menu button is clicked the menu appears, but when the X is clicked nothing happens. How can I restore it to

Click to close the menu

Dennis Marceau I want to close the menu by clicking a link or clicking outside the menu. To keep things nice and light, I don't want to use jQuery. what should I do? <nav class="nav"> <div class="nav__left"> <h3><a href="#home">DenisMasot</a></h3> </di

Click to close the menu

Dennis Marceau I want to close the menu by clicking a link or clicking outside the menu. To keep things nice and light, I don't want to use jQuery. what should I do? <nav class="nav"> <div class="nav__left"> <h3><a href="#home">DenisMasot</a></h3> </di

Click on X to close the menu

sambaileyy : I struggled to get the X, removed the active state class's menu, and returned to the "hidden" state. Where am I going wrong in JS? When the menu button is clicked the menu appears, but when the X is clicked nothing happens. How can I restore it to

Close menu on click event

Kayana I used the following code to create a hamburger menu that slides in from the left and covers the entire screen. Some of my menu links are regular links to other pages, while others are anchor links below the home page. However, when the anchor link is c

Click on X to close the menu

sambaileyy : I struggled to get the X, removed the active state class's menu, and returned to the "hidden" state. Where am I going wrong in JS? When the menu button is clicked the menu appears, but when the X is clicked nothing happens. How can I restore it to

Click to close the menu

Dennis Marceau I want to close the menu by clicking a link or clicking outside the menu. To keep things nice and light, I don't want to use jQuery. what should I do? <nav class="nav"> <div class="nav__left"> <h3><a href="#home">DenisMasot</a></h3> </di

Click to close the menu

Dennis Marceau I want to close the menu by clicking a link or clicking outside the menu. To keep things nice and light, I don't want to use jQuery. what should I do? <nav class="nav"> <div class="nav__left"> <h3><a href="#home">DenisMasot</a></h3> </di

Close menu when click/touch external javascript

Rab 77 The css/javascript trigger menu doesn't close when someone clicks or touches outside . You have to press the menu button to turn it off. I tried some jquery functions like: $('#menucontainer').click(function(event) { event.stopPropagation(); }); Bu

close my span when i click a in the menu

Karl I made a responsive menu a few days ago and it worked perfectly except when I clicked my a in the menu-ul I created, it closed the menu but not the state of my hamburger menu span. Here is the code so you can understand function openNav(){ document.ge

Close menu when click is detected outside

OJM I am trying to close the menu when the user clicks outside the element. I've tried many solutions from Stack Overflow and none of them worked for me. The user should be able to click on the menu button (3 dots) and the menu will be displayed. Then if they

Open the dropdown menu on click on the sidebar

Prites I am working on a project and I have created a navbar. Also present in this dropdown menu. In this drop down menu, there is hover effect. Now I try to click event and open and close the submenus of the dropdown . But it didn't work. I only use HTML and

Open the dropdown menu on click on the sidebar

Prites I am working on a project and I have created a navbar. Also present in this dropdown menu. In this drop down menu, there is hover effect. Now I try to click event and open and close the submenus of the dropdown . But it didn't work. I only use HTML and

One click, set the sidebar active menu item to class="active"?

pizza sauce I made a recent post here and closed it after fixing the first issue, but still can't fix this. I have a sidebar with links such as "Clients", "Projects" etc. They link to the corresponding pages. However, I want to be able to set for the menu item

One click, set the sidebar active menu item to class="active"?

pizza sauce I made a recent post here and closed it after fixing the first issue, but still can't fix this. I have a sidebar with links such as "Clients", "Projects" etc. They link to the corresponding pages. However, I want to be able to set for the menu item

One click, set the sidebar active menu item to class="active"?

pizza sauce I made a recent post here and closed it after fixing the first issue, but still can't fix this. I have a sidebar with links such as "Clients", "Projects" etc. They link to the corresponding pages. However, I want to be able to set for the menu item

Move menu - click outside menu to close menu

Julian Camilleri I have that button on my mobile site; the problem is, I need to add a way for the menu to close when the user clicks or taps outside the menu. Can someone guide me? cruel Fiddle link : http://jsfiddle.net/eAGjM/ You need to check that the clic