0

I want the sidebar to close when cicked outside the sidebar.

Right now the sidebar closes when pressed inside the sidebar.

Please checkout the code below. I have a code sample below.

  var mySidenav = document.getElementById("mySidenav");
  
  var navigationOpen = false;

  function openNav() {
    document.getElementById("mySidenav").style.width = "300px";
    document.getElementById("navIcon").style.visibility = "hidden";
    navigationOpen = true;
  }

  function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("navIcon").style.visibility  = "visible";
    navigationOpen = false;
  }
    //Instead of when clicking on the nav to close
    // Make it close when clicking outside of the nav
    window.onclick = function(event) {
          if (event.target == mySidenav) {
        console.log("mouse click2")
        closeNav();
      }
      
      
  }
  
  
.sidenav {
    padding-top: 60px;
    overflow-x: hidden;
    overflow-y: auto;
    height: 100%;
    width: 0;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff000;
    transition: 0.5s;
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>

  <div id="navIcon" onclick="openNav()"> &#9776;</div>
  
<div id="mySidenav" class="sidenav">

  
</div>
</body>
</html>

Thank you for your time!

AttackTheWar
  • 219
  • 1
  • 12
  • Does this answer your question? [Close the menu on click](https://stackoverflow.com/questions/42201156/close-the-menu-on-click) – Calvin Nunes Feb 13 '20 at 20:41
  • Hi Rai, Not really I got the nav opening working when pressing "open" and the closing working when pressing a link. I want to close the nav when clicking outside of the navigator – AttackTheWar Feb 13 '20 at 20:46

3 Answers3

0

Maybe you want to try this


    if (event.target !== mySidenav) {
      console.log("mouse click2")
      closeNav();
      return;
    }
    openNav();
Ian Guimarães
  • 371
  • 3
  • 12
0
function Opennav(). 
   {document.getElementById('mySidenav').style.display = 
      'block'; // show
     }
function Closenav(){
    document.getElementById('mySidenav').style.display = 
        'none'; // hide
       }

Update your function:

 var sidenav= document.getElementById('mySidenav');


  window.onclick = function(event) {
    console.log("mouse click")
      if (event.target == sidenav) {
         console.log("mouse click2")
       closeNav();
       }
     }
upsidedownwf
  • 134
  • 1
  • 3
  • 12
0

I added some style code and change bit of js code. try it

    var mySidenav = document.getElementById("mySidenav");

    var navigationOpen = false;

    function openNav() {
        document.getElementById("mySidenav").style.width = "300px";
        document.getElementById("navIcon").style.visibility = "hidden";
        navigationOpen = true;
    }

    function closeNav() {
        document.getElementById("mySidenav").style.width = "0";
        document.getElementById("navIcon").style.visibility  = "visible";
        navigationOpen = false;
    }
    //Instead of when clicking on the nav to close
    // Make it close when clicking outside of the nav
    window.onclick = function(event) {
        if (event.target == document.body) {
            closeNav();
        }
    }
        html, body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
        .sidenav {
            padding-top: 60px;
            overflow-x: hidden;
            overflow-y: auto;
            height: 100%;
            width: 0;
            position: fixed;
            top: 0;
            left: 0;
            background-color: #fff000;
            transition: 0.5s;
        }
        #navIcon {
            padding: 4px 16px;
            cursor: pointer;
        }
<!DOCTYPE html>
<html lang="en">
<head>

</head>
<body>

<div id="navIcon" onclick="openNav()"> &#9776;</div>

<div id="mySidenav" class="sidenav">


</div>

</body>
</html>
Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36