skip to Main Content

I am trying to make a mobile menu for a responsive webpage. When I try to click my mobile menu icon, the dropdown section doesn’t appear and nothing happens. I feel like my code is correct and have combed through it multiple times but I have hit a dead-end. Any help would be greatly appreciated as I am still kind of new to HTML/CSS/JS

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>The Panel Guys</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <nav class="navbar">
      <div class="navbar__container">
        <a href="/" id="navbar__logo">The Panel Guys</a>
        <div class="navbar__toggle" id="mobile-menu">
          <span class="bar"></span>
          <span class="bar"></span>
          <span class="bar"></span>
        </div>
        <ul class="navbar__menu">
          <li class="navbar__item">
            <a href="/" class="navbar__links"> Home </a>
          </li>
          <li class="navbar__item">
            <a href="about.html" class="navbar__links"> About </a>
          </li>
          <li class="navbar__item"> 
            <a href="/" class="navbar__links"> Gallery </a>
          </li>
          <li class="navbar__btn">
            <a href="/" class="button"> Contact </a>
          </li>
        </ul>
      </div>
    </nav>
    <script src="app.js"></script>

CSS:

@media screen and (max-width: 960px) {
    .navbar__container {
        display: flex;
        justify-content: space-between;
        height: 80px;
        z-index: 1;
        width: 100%;
        max-width: 1300;
        padding: 0;
    }

    .navbar__menu {
        display: grid;
        grid-template-columns: auto;
        margin: 0;
        width: 100%;
        position: absolute;
        top: -1000px;
        opacity: 0;
        transition: all 0.5s ease;
        height: 50vh;
        z-index: -1;
        background: #fff;
        
        
    }

    .navbar__menu.active {
        background: #131313;
        top: 100%;
        opacity: 1;
        transition: all 0.5 ease;
        z-index: 99;
        height: 50vh;
        font-size: 1.6rem;
    }

    #navbar__logo {
        padding-left: 25px;
        font-size: 25px;
    }

    .navbar__toggle .bar {
        width: 25px;
        height: 3px;
        margin: 5px auto;
        transition: all 0.3s ease-in-out;
        background: black;
    }

    .navbar__item {
        width: 100%;
    }

    .navbar__links {
        text-align: center;
        padding: 2rem;
        width: 100%;
        display: table;
    }

    #mobile-menu {
        position: absolute;
        top: 20%;
        right: 5%;
        transform: translate(5%, 20%);
    }

    .navbar__btn {
        padding-bottom: 2rem;
    }

    .button {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 80%;
        height: 80px;
        margin: 0;
    }

    .navbar__toggle .bar {
        display: block;
        cursor: pointer;
    }

    #mobile-menu.is-active .bar:nth-child(2) {
        opacity: 0;
    }

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

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

And JS:

const menu = document.querySelector("#mobile=menu");
const menuLinks = document.querySelector(".navbar__menu");

menu.addEventListener("click", function () {
  menu.classList.toggle("is-active");
  menuLinks.classList.toggle("active");
});

2

Answers


  1. you misspelled the id name of the menu in app.js. As per your code, the id is "#mobile-menu" and not "#mobile=menu" you putted = instead of -.

    **The updated version of the app.js: **

    const menu = document.querySelector("#mobile-menu"); #changed the #mobile=menu -> #mobile-menu
    const menuLinks = document.querySelector(".navbar__menu");
    
    menu.addEventListener("click", function () {
      menu.classList.toggle("is-active");
      menuLinks.classList.toggle("active");
    });
    Login or Signup to reply.
  2. You need to change #mobile=menu to #mobile-menu in js

    const menu = document.querySelector("#mobile-menu");
    const menuLinks = document.querySelector(".navbar__menu");
    
    menu.addEventListener("click", function () {
      menu.classList.toggle("is-active");
      menuLinks.classList.toggle("active");
    });
    @media screen and (max-width: 960px) {
        .navbar__container {
            display: flex;
            justify-content: space-between;
            height: 80px;
            z-index: 1;
            width: 100%;
            max-width: 1300;
            padding: 0;
        }
    
        .navbar__menu {
            display: grid;
            grid-template-columns: auto;
            margin: 0;
            width: 100%;
            position: absolute;
            top: -1000px;
            opacity: 0;
            transition: all 0.5s ease;
            height: 50vh;
            z-index: -1;
            background: #fff;
            
            
        }
    
        .navbar__menu.active {
            background: #131313;
            top: 100%;
            opacity: 1;
            transition: all 0.5 ease;
            z-index: 99;
            height: 50vh;
            font-size: 1.6rem;
        }
    
        #navbar__logo {
            padding-left: 25px;
            font-size: 25px;
        }
    
        .navbar__toggle .bar {
            width: 25px;
            height: 3px;
            margin: 5px auto;
            transition: all 0.3s ease-in-out;
            background: black;
        }
    
        .navbar__item {
            width: 100%;
        }
    
        .navbar__links {
            text-align: center;
            padding: 2rem;
            width: 100%;
            display: table;
        }
    
        #mobile-menu {
            position: absolute;
            top: 20%;
            right: 5%;
            transform: translate(5%, 20%);
        }
    
        .navbar__btn {
            padding-bottom: 2rem;
        }
    
        .button {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 80%;
            height: 80px;
            margin: 0;
        }
    
        .navbar__toggle .bar {
            display: block;
            cursor: pointer;
        }
    
        #mobile-menu.is-active .bar:nth-child(2) {
            opacity: 0;
        }
    
        #mobile-menu.is-active .bar:nth-child(1) {
            transform: translateY(8px) rotate(45deg);
        }
    
        #mobile-menu.is-active .bar:nth-child(3) {
            transform: translateY(-8px) rotate(-45deg);
        }
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>The Panel Guys</title>
        <link rel="stylesheet" href="styles.css" />
      </head>
      <body>
        <nav class="navbar">
          <div class="navbar__container">
            <a href="/" id="navbar__logo">The Panel Guys</a>
            <div class="navbar__toggle" id="mobile-menu">
              <span class="bar"></span>
              <span class="bar"></span>
              <span class="bar"></span>
            </div>
            <ul class="navbar__menu">
              <li class="navbar__item">
                <a href="/" class="navbar__links"> Home </a>
              </li>
              <li class="navbar__item">
                <a href="about.html" class="navbar__links"> About </a>
              </li>
              <li class="navbar__item"> 
                <a href="/" class="navbar__links"> Gallery </a>
              </li>
              <li class="navbar__btn">
                <a href="/" class="button"> Contact </a>
              </li>
            </ul>
          </div>
        </nav>
        <script src="app.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search