Home > database >  hamburger button not appearing when resizing window
hamburger button not appearing when resizing window

Time:08-15

I tried to create just a simple responsive hambuger button nothing more but when I resize the window to a smaller one the hamburger button does not appear.

When I open the console this error message appears. I searched everywhere and nothing worked for me

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

although it is a basic HTML and CSS, I can't figure where the error is.

what's the error ?

const hamburger = document.querySelector(".hambuger");
const navMenu = document.querySelector(".nav-menu");

hamburger.addEventListener("click", () => {
  hamburger.classList.toggle("active");
  navMenu.classList.toggle("active");
});

document.querySelectorAll("nav-link").forEach((n) =>
  n.addEventListener("click", () => {
    hamburger.classList.remove("active");
    navMenu.classList.remove("active");
  })
);
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

header {
  background-color: #262626;
}

li {
  list-style: none;
}

a {
  color: white;
  text-decoration: none;
}

.navbar {
  min-height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
}
.nav-menu {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 60px;
}

.nav-branding {
  font-size: 2rem;
}

.nav-link {
  transition: 0.7s ease;
}

.nav-link:hover {
  color: dodgerblue;
}

.hamburger {
  display: none;
  cursor: pointer;
}

.bar {
  display: block;
  width: 25px;
  height: 3px;
  margin: 5px auto;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
  background-color: white;
}

@media (max-width: 768px) {
  .hanburger {
    display: block;
  }

  .hamburger.active .bar:nth-child(2) {
    opacity: 0;
  }

  .hamburger.active .bar:nth-child(1) {
    transform: translateY(8px) rotate(45deg);
  }

  .hamburger.active .bar:nth-child(3) {
    transform: translateY(-8px) rotate(-45deg);
  }

  .nav-menu {
    position: fixed;
    left: -100%;
    top: 70px;
    gap: 0;
    flex-direction: column;
    background-color: #262626;
    width: 100%;
    text-align: center;
    transition: 0.3s;
  }

  .nav-item {
    margin: 16px 0;
  }

  .nav-item.active {
    left: 0;
  }
}
<!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" />
    <link rel="stylesheet" href="style.css" />
    <title>document</title>
  </head>
  <body>
    <header>
      <nav >
        <a href="#" >DEV.</a>
        <ul >
          <li >
            <a href="#" >Home</a>
          </li>
          <li >
            <a href="#" >About</a>
          </li>
          <li >
            <a href="#" >Contact</a>
          </li>
        </ul>
        <div >
          <span ></span>
          <span ></span>
          <span ></span>
        </div>
      </nav>
    </header>
    <script src="script.js"></script>
  </body>
</html>

CodePudding user response:

const hamburger = document.querySelector(".hambuger");
const navMenu = document.querySelector(".nav-menu");

here, you probably mistyped ".hamburger". because of this, the eventlistener of hamburger class returns null.

  • Related