Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

On first click, there is no response from Navigation bar

Purpose of this code:

Navigation bar on the right which "opens" once clicked on, and "closes" once clicked on again, but also "closes" when clicked anywhere on the page once its "open".

Issues right now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. After running the code, on first click there is no response from navigation bar, then once clicked again it opens, and once clicked after that it closes which are intended.

  2. I want the navigation bar to close when clicked on the navigation bar itself (which it does currently), but also close when clicking anywhere on the page itself(having issues with how to do this currently)

window.addEventListener("load", onLoadHandler);

function onLoadHandler(e) {
  let side = document.getElementById("mySidenav", moveNav);
  side.addEventListener("click", moveNav)
}

function moveNav() {
  const menu = document.getElementById('mySidenav')
  menu.style.width = menu.style.width === '100px' ? '250px' : '100px'
}
body {
  font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 100px;
  position: fixed;
  z-index: 1;
  top: 0;
  right: 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;
}

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

.sidenav a:hover {
  color: #f1f1f1;
}

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

>Solution :

This moves the state of the nav to a toggled variable, allowing us to use it in any function. On mySideNav click we toggle the toggled value, and stop propagation so that no other event listeners are triggered. We then add an additional event listener on window, that sets toggled to false. This will only get called if you’re clicking outside of mySideNav.

There are definitely some other improvements we can make here but hopefully that helps in someway.

(function(){
let toggled = false;

window.addEventListener("load", onLoadHandler);

function onLoadHandler(e) {
  let side = document.getElementById("mySidenav");
  side.addEventListener("click", toggleNav);

  window.addEventListener('click', () => {
    if(toggled) { 
      toggled = false;
      moveNav();
    }
  });
}

function moveNav(e) {
  event.stopPropagation();
  const menu = document.getElementById('mySidenav');
  menu.style.width = toggled ? '250px' : '100px'
}

function toggleNav(e) {
  toggled = !toggled;
  moveNav(e);
}
})()
body {
  font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 100px;
  position: fixed;
  z-index: 1;
  top: 0;
  right: 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;
}

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

.sidenav a:hover {
  color: #f1f1f1;
}

@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<div id="mySidenav" class="sidenav">
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading