0

$(document).ready(function () {

    openNav();
    $("#main span").click(function (e) {
        //    $( "#main .opener" ).on( "click", notify );
        openNav();
        e.stopPropagation();
            });
    });

//This line helpfully tells you exactly which dom element was clicked!
// $( "*", document.body ).click(function( event ) {

//This line produces same result as "body": neither permit clicking too far below the last element on the page, in this case the #MainDiv
$(document.body ).click(function( event ) {
  event.stopPropagation();
  var domElement = $( this ).get( 0 );
    closeNav();
  alert(domElement.nodeName);
});

//slightly different version below
//Seems we don't need document.ready
/*
$(document).ready(function () {
    $("body").click(function () {
        //    $( "#main .opener" ).on( "click", notify );
        closeNav();
        //console.log( $( this ).text() );
        // alert('hello');
        // e.stopPropagation();
    });
    // closeNav();
    // this stops the event from bubbling up to the body
});
*/



function openNav() {
    //event.stopPropagation(); // this stops the event from bubbling up to the body
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
    // event.stopPropagation(); // this stops the event from bubbling up to the body
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft = "0";
    document.body.style.backgroundColor = "white";
    //e.stopPropagation(); // this stops the event from bubbling up to the body
};
body {
  font-family: "Lato", sans-serif;
  transition: background-color .5s;
}

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

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

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

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

@media screen and (max-height: 450px) {
  .sidenav {
    padding-top: 15px;
  }
  .sidenav a {
    font-size: 18px;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Jquery testing</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <div id="mySidenav" class="sidenav">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
  <div id="main">
    <h2>Sidenav Push Example</h2>
    <p>Click on the element below to open the side navigation menu, and push this content to the right. Notice that we add a black see-through background-color to body when the sidenav is opened.</p>
    <span class="opener" style="font-size:30px;cursor:pointer">&#9776; open</span>
  </div>



  <script src="myclick.js"></script>
</body>

</html>

I'm trying to attach a click event to the body that fires a preexisting sidebar close function aptly called CloseNav() without breaking the OpenNav() sidebar function which opens the sidebar. I copied the code from here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sidenav_push_opacity

I tried making it on Codepen: https://codepen.io/CodusMusicus/pen/aqLwQQ The sidebar with opacity works. Attempts to add a body close function break everything.

I made a Fiddle THAT WORKS but only once! It opens on page load and closes with a body click. But then it breaks. https://jsfiddle.net/359ns4uv/4/

I've tried to stop bubbling of events using e.stopPropagation(); but no luck.

Thank you! Code below

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jquery testing</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<style>
body {
    font-family: "Lato", sans-serif;
    transition: background-color .5s;
}

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

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

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

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

@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>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right. Notice that we add a black see-through background-color to body when the sidenav is opened.</p>
  <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
</div>


    <script>
$('body:not(#main, #mySidenav, .sidenav)').click(function() {
        // do something here like:
     //  alert('hey! The body click is working!!!');
    closeNav();
      });
    </script>

    <script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}

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


</script>



</body>

</html>
CodeNotes
  • 11
  • 2
  • The problem is probably because of event bubbling. You need to use `event.stopPropagation()` when you click on something that shouldn't trigger the click on `body`. – Barmar Feb 15 '18 at 01:04
  • `$('#mySidenav').style.width = "250px";` is not valid. `.style` is a DOM property, not a jQuery property. It would have to be `$("#mySidenav").css("width", "250px")` – Barmar Feb 15 '18 at 01:05
  • I tried every possible permutation of event.stopPropagation() in my fiddle. If it's not too much trouble, could you show me what I'm going wrong? : https://jsfiddle.net/359ns4uv/4/ I know I'm missing something minor but can't get this to work. – CodeNotes Feb 15 '18 at 04:08
  • It's so CLOSE! I made a Fiddle THAT WORKS but only once! It opens on page load and closes with a body click. But then it breaks. https://jsfiddle.net/359ns4uv/4/ – CodeNotes Feb 15 '18 at 10:03
  • Could you use [Stack Snippet](https://stackoverflow.blog/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) so we don't have to go to an external site? – Barmar Feb 15 '18 at 17:59
  • Hi Barmar! Thanks for helping! I have a working Stack Snippet, but it just works on load: Navbar opens and even closes by clicking on the body, however afterwards, all apparent functions cease. – CodeNotes Feb 15 '18 at 18:16
  • You're missing the `e` parameter in the `#main span` click handler, so `e.stopPropagation()` gets an error. – Barmar Feb 15 '18 at 21:26
  • You don't need `stopPropagation` in the `body` handler -- that's the end of bubbling. – Barmar Feb 15 '18 at 21:27
  • Thank you Barmar! That fixed the disabling of the OpenNav handler, however, if I click low on the body (I have a huge monitor with lots of body white space below) I cannot activate a close. It only works with the NAV X or high up on the body, above the main div. Why does CloseNav not activate anywhere on the body, especially in blank unoccupied spaces? I tried limiting the height of other elements and making body 100% height, but no luck. Body is already 100% height by default but I tried it anyway. – CodeNotes Feb 16 '18 at 01:07
  • Try clicking anywhere, high and low and notice the alert! I reworked the closeNav click event adding a listener which identifies which DOM element was clicked. If one clicks toward the top of the page or just beneath the last element (div#Main) it works, the nav bar closes and the alert informs the BODY was clicked. But if one clicks below this in the white space, but still in the body, the listener pretends you are not in the body and nothing happens. Ideally clicking anywhere in the browser space should close this nav bar. Alas, not so...yet. – CodeNotes Feb 16 '18 at 02:27
  • Locally I added some filler text and this makes clicking anywhere in or directly around that element function as expected: the navbar closes. However, if there is much white space in the bottom of the browser, for all intents and purposes, that is not considered the body despite being prominent in the browser display window and containing a background color. If you can see background color, you can see the DOM body, right? Can I call this a jquery bug/feature? And features need...hacks! – CodeNotes Feb 16 '18 at 03:08
  • How can I make the whole BODY from the user's perspective, wherever the user can click, fair game to close the nav bar? Or said a different way: how do I extend the BODY to all visible portions of the page on which a user can click? – CodeNotes Feb 16 '18 at 03:35
  • Maybe you should look at: https://stackoverflow.com/questions/152975/how-to-detect-a-click-outside-an-element – Barmar Feb 16 '18 at 16:32
  • Thank you Barmar! That thread had a few different ways to address the body click with document or window or (html) or "closest." VERY HELPFUL so consider this case closed! Thank you Barmar for taking the time to help! :-) – CodeNotes Feb 17 '18 at 06:53

0 Answers0