Home > database >  Responsive navigation bar not displayed (hamburger menu)
Responsive navigation bar not displayed (hamburger menu)

Time:09-05

I am trying to make the so-called "Hambuger menu" which appears very well but when I click it nothing happens.

I have an event listener to listen for any click on that button and then according it would toggle the class to show or hide the ul elements.

I can't find the mistake myself. Is there even a simpler way?

const toggleButton = document.getElementsByClassName('toggle-button')[0];
const navbarLinks = document.getElementsByClassName('navbar-links')[0];

toggleButton.addEventListener('click' ,function (){
    navbarLinks.classList.toggle('active')
});
/* Basic/Boiler css */
*{
  box-sizing: border-box;
}
body{
  margin: 0;
  padding: 0;
}
/* nav bar  */
.navbar{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #000;
  color: white;
}
.navbar-logo{
  width: 10rem;
  height: 3rem;
  margin: 0.5rem;
  border-radius: 4px;
}
.navbar-links ul{
  margin: 0;
  padding: 0;
  display: flex;
}
.navbar-links li{
  list-style: none;
  transition: font-size 0.5s ease-out 100ms;
}
.navbar-links li a{
  text-decoration: none;
  color: white;
  padding-right: 1rem;
  display: block;

}
.navbar-links li:hover{
 font-size: 1.4rem;
}
/* Responsive Navbar */
.toggle-button{
  position: absolute;
  top: .75rem;
  right: 1rem;
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
}
.toggle-button .bar{
  height: 3px;
  width: 100%;
  background: white;
  border-radius: 10px;
}
@media (max-width: 650px){
  .toggle-button{
      display: flex;
  }
  .navbar-links{
      width: 100%;
  }
  .navbar-links ul{
      flex-direction: column;
      width: 100%;
      display: none;
  }
  .navbar-links li a{
      padding: 10px;
  }
  .navbar-links li{
      text-align: center;
  }
  .navbar{
      flex-direction: column;
      align-items: flex-start;

  }
  .navbar-links.active{
      display: flex;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="icon" href="/logo.png" type="image/jpg">
  <link rel="stylesheet" href="style.css">
</head>
<body>
<nav >
    <div ><img  src="/logo.png" alt="logo"></div>
    <a href="#" >
      <span ></span>
      <span ></span>
      <span ></span>
    </a>
    <div >
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact</a></li>
    </ul>
  </div>
</nav>
  <script src="front.js"></script>
</body>
</html>

I repeat Again The hamburger appers alright but when i click it it dose not respond

CodePudding user response:

You have set the style for the ul as display: none. display: none needs to be overridden when the active class is applied.

Add this rule ul {display: flex} at the end of the media query, your code should look something like this:

@media (max-width: 650px){
  ...
  .navbar-links.active ul{
    display: flex
  }
}

CodePudding user response:

You forgot to add class "active" in your CSS file. Also, your navbar-links should have "display:none;" instead of its list.

const toggleButton = document.getElementsByClassName('toggle-button')[0];
const navbarLinks = document.getElementsByClassName('navbar-links')[0];

toggleButton.addEventListener('click' ,function (){
    navbarLinks.classList.toggle('active');
});
  /* Basic/Boiler css */
*{
    box-sizing: border-box;
}
body{
    margin: 0;
    padding: 0;
}

  /* nav bar  */
.navbar{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #000;
    color: white;
}
.navbar-logo{
    width: 10rem;
    height: 3rem;
    margin: 0.5rem;
    border-radius: 4px;
}
.navbar-links ul{
    margin: 0;
    padding: 0;
    display: flex;
}
.navbar-links li{
    list-style: none;
    transition: font-size 0.5s ease-out 100ms;
}
.navbar-links li a{
    text-decoration: none;
    color: white;
    padding-right: 1rem;
    display: block;

}
.navbar-links li:hover{
   font-size: 1.4rem;
}
  /* Responsive Navbar */
.toggle-button{
    position: absolute;
    top: .75rem;
    right: 1rem;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    width: 30px;
    height: 21px;
}
.toggle-button .bar{
    height: 3px;
    width: 100%;
    background: white;
    border-radius: 10px;
}

@media (max-width: 650px){
    .toggle-button{
        display: flex;
    }
    .navbar-links{
        width: 100%;
        display:none;
    }
  .active {
    display: block;
    padding: 25px;
    color: white;
    font-size: 25px;
    box-sizing: border-box;
  } 
    .navbar-links ul{
        flex-direction: column;
        width: 100%;
    }
    .navbar-links li a{
        padding: 10px;
    }
    .navbar-links li{
        text-align: center;
    }
    .navbar{
        flex-direction: column;
        align-items: flex-start;

    }
    .navbar-links .active{
        display: flex;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="icon" href="/logo.png" type="image/jpg">
  <link rel="stylesheet" href="style.css">
</head>
<body>
<nav >
    <div ><img  src="/logo.png" alt="logo"></div>
    <a href="#" >
      <span ></span>
      <span ></span>
      <span ></span>
    </a>
    <div >
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact</a></li>
    </ul>
  </div>
</nav>
  <script src="front.js"></script>
</body>
</html>

  • Related