Closing an overlay outside the overlay as well

Joined
Dec 11, 2022
Messages
1
Reaction score
0
Hi: Here, I am trying to open an overlay window, then, close the window outside the window as well. That I have seen possible in some websites. As soon as I take out // in closeNav() in my last section, the window doesn't open. Thanks for your helps.

HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    html,
    body {
    margin: 0;
        }
    .sidenav {
        height: 72%;
        width: 0;
        position: fixed;
        top: 0;
        right:0;
        scroll-margin-right: 0;
        background-color: #6a6c6d;
        overflow-x: hidden;
        transition: 0.5s;
        padding-top:50px;
        }
    .sidenav a {
        padding: 8px 8px 10px 15px;
        text-decoration: none;
        font-size: 16px;
        color: black ;
        display: block;
        transition: 0.3s;
        }
    .sidenav a:hover {
        color: #f1f1f1;
        }
    .sidenav .closebtn {
        position: absolute;
        top: 0;
        right: 25px;
        font-size: 50px;
        }
    #container{
        display: flex;
        padding: 0px 0px 0px 0px;
        }
    @media screen and (max-height: 450px) {
        .sidenav {padding-top: 0px;}
        .sidenav a {font-size: 60px;} 
        }
</style>
</head>
<body>
<div id="mySidenav">
    <div class="sidenav" id="sidenav">
        <a href="javascript:void(0)"  id="closebtn" class="closebtn" onclick="closeNav()"> &times;</a>
           <a href="xx.html">  Mission</a>
        <a href="#">Services</a>
    </div>
</div>
<div  id="BodyTexts">
    <span style="font-size:30px;cursor:pointer" onclick="openNav()">
        &#9776;Menu</span>
    <p>Learning javascript aa</p>
</div>
<script>
    // var sidenav = document.getElementById("sidenav");
    var OStatus;
      
    function openNav() {
        document.getElementById("sidenav").style.width = "30%";
        OStatus = "opened"
    }
    function closeNav() {
        document.getElementById("sidenav").style.width = "0";
        OStatus = "      "
    }
    window.onclick = function(event) {
       if ( OStatus = "opened" && event.target != sidenav )  {
        //  closeNav();
       Ostatus = "      "
       }
    }
    
</script> 
</body>
</html>
Code:
 
Joined
Jul 12, 2020
Messages
89
Reaction score
9
It's not opening because you're literally telling it not to. Clicking anywhere on the window takes precedence over clicking on a link.
JavaScript:
window.onclick = function(event)
...
<a href="#nogo"  onclick="closeNav()"> &times;</a>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top