skip to Main Content

My nav bar isn’t working. I don’t know why. The javascript functions are doing opposite of what they are supposed to do. Also the open button isn’t being seen. The x button does work. When I do close the navBar the open button isn’t there. I have to refresh my screen for the box to appear again.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="script.js"></script>
    <link href="style.css" rel="stylesheet">
    <title>IKC Website</title>
</head>
<body>

    <span class="openNav" onclick="close()">☰ open</span>

    <div id ="sideNav" class="sideNav">
        <a href="javascript:void(0)" class="closeButton" onclick="open()">&times;</a>
        <a href="about.html">About<a>
        <a href="members.html">Members<a>
        <a href="services.html">Services<a>
        <a href="contactUs.html">Contact Us<a>
    </div>
</body>
</html>

CSS


body {
    margin: 0;
    padding: 0;
    font-family: 'Lato', sans-serif;
}

.openNav {
    font-size: 30px;
    cursor: pointer;
    position: relative;
    top: 100px;
    left: 100px;
    margin: 10px;
    z-index: 0;
    color: black;
}

.sideNav {
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgb(0, 0, 0);
    width: 55vh;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 1;
    overflow-x: hidden;
    transition: 0.5s;
}

.sideNav a {
    color: #818181;
    text-decoration: none;
    font-size: 40px;
    padding: 10px;
    transition: 0.3s;
    display: block;
}

.sideNav .closeButton {
    color: #818181;
    width: 100px;
    margin-left: 45vh;
    position: relative;
    left: 1vh;
    bottom: 17vh;
    margin-bottom: 0;
}

.sideNav a:hover{
    color: white;
}

@media screen and (max-height: 450px) {
    .sideNav {
        padding-top: 50px;
    }
    .sideNav a {
        font-size: 18px;
    }
}

Javascript

function open() {
    document.getElementById("sideNav").style.width = "250px";
}

function close() {
    document.getElementById("sideNav").style.width = "0px";
}

2

Answers


  1. Your "On" button is a child of the navigation bar, and of course disappears when you close the navigation bar.Causes the entry to open the navigation bar to disappear.

    You should:

        <span class="openNav" onclick="close()">&#9776; open</span>
    
        <div id ="sideNav" class="sideNav">
            <a href="about.html">About<a>
            <a href="members.html">Members<a>
            <a href="services.html">Services<a>
            <a href="contactUs.html">Contact Us<a>
        </div>
    
        <span class="closeButton" onclick="open()">&times;</span>
    

    or use "toggle" on "openNav" to dynamically bind the class name to open or close it

    Login or Signup to reply.
  2. try my code below:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <!-- <script src="script.js"></script>
      <link href="style.css" rel="stylesheet"> -->
      <title>IKC Website</title>
    </head>
    
    <body>
      <style>
        body {
          margin: 0;
          padding: 0;
          font-family: 'Lato', sans-serif;
        }
    
        .openNav {
          font-size: 30px;
          cursor: pointer;
          position: relative;
          top: 100px;
          left: 100px;
          margin: 10px;
          z-index: 0;
          color: black;
        }
    
        .sideNav {
          position: fixed;
          top: 0;
          left: 0;
          background-color: rgb(0, 0, 0);
          width: 55vh;
          height: 100vh;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          z-index: 1;
          overflow-x: hidden;
          transition: 0.5s;
        }
    
        .sideNav a {
          color: #818181;
          text-decoration: none;
          font-size: 40px;
          padding: 10px;
          transition: 0.3s;
          display: block;
        }
    
        .sideNav .closeButton {
          color: #818181;
          width: 100px;
          margin-left: 45vh;
          position: relative;
          left: 1vh;
          bottom: 17vh;
          margin-bottom: 0;
        }
    
        .sideNav a:hover {
          color: white;
        }
    
        @media screen and (max-height: 450px) {
          .sideNav {
            padding-top: 50px;
          }
    
          .sideNav a {
            font-size: 18px;
          }
        }
      </style>
    
      <span class="openNav" onclick="openFn()">&#9776; open</span>
    
      <div id="sideNav" class="sideNav">
        <a class="closeButton" onclick="closeFn()">&times;</a>
        <a href="about.html">About</a>
        <a href="members.html">Members</a>
        <a href="services.html">Services</a>
        <a href="contactUs.html">Contact Us</a>
      </div>
      <script>
        function openFn() {
          document.getElementById("sideNav").style.width = "55vh";
        }
    
        function closeFn() {
          document.getElementById("sideNav").style.width = "0px";
        }
      </script>
    </body>
    
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search