search

Home  >  Q&A  >  body text

JavaScript is not running as expected

I'm trying to make html css js responsive but it's not working as expected, when it minimizes it's already responsive but when onclick it doesn't work. Navigation and navigation bar do not appear and disappear when clicked. html and css are working But js I don't know what is wrong or wrong Can anyone help me?

let menu = document.querySelector('#menu-icon');
let navbar = document.querySelector('.navbar');

console.log(menu); // cek "menu-icon" 
console.log(navbar); // cek "navbar"

menu.onclick = () => {
    menu.classList.toggle("bx-x");
    navbar.classList.toggle("open");
}
#menu-icon
{
    font-size: 35px;
    color: var(--text-color);
    cursor: pointer;
    z-index: 10001;
    display: none;
}


/* ini untuk mengatur tampilan hp */
@media (max-width: 1280px)
{
    header
    {
        padding: 14px 2%;
        transition: .2s;
    }
    .navbar a
    {
        padding: 5px 0;
        margin: 0px 20px;
    };
}

@media (max-width: 1090px)
{
    #menu-icon
    {
        display: block;
    }   
    .navbar
    {
        position: absolute;
        top: 100%;
        right: -100%;
        width: 270px;
        height: 29hv;
        background: var(--main-color);
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        border-radius: 10px;
        transition: all .50s ease;
    }

    .navbar .open
    {
        right: 2%;
    }
}
    
      <ul class="navbar">
        <li><a href="#" class="active">HOME</a></li>
        <li><a href="#">EVENT</a></li>
        <li><a href="#">ABOUT</a></li>
      </ul>
      <div class="main">
        <div class="bx bx-menu" id="menu-icon"></div>
      </div>
    </header>
    <!-- js link -->
    <script type="text/javascript" src="js/script">
    </script>

P粉005134685P粉005134685276 days ago444

reply all(1)I'll reply

  • P粉605385621

    P粉6053856212024-04-05 00:17:12

    The error occurs in your CSS:

    .navbar .open

    Change it to:

    .navbar.open

    The rule appearing in the code specifies an element with class open that has an ancestor with class navbar. The rule you want is to specify an element with class bothnavbar and open.

    reply
    0
  • Cancelreply