Search code examples
javascripttoggleaddeventlistenersidebar

Why can't i toggle between my sidebar and navbar?


I'm making a sidebar slider on the left but it doesn't work. I don't know if it's because my codes is missing something or wrong somewhere...it'd nice of you to help completing my codes! Thank you so much!

This is my html:

<!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="stylesheet" href="style.css">
    <script src="https://kit.fontawesome.com/48a972c999.js" crossorigin="anonymous"></script>

</head>

<body>
    <div class="navbar">
        <i class="fas fa-bars" onclick="active()"></i>
        <div class="sidebar">
            <i class="fas fa-times" onclick="active()"></i>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
    </div>

    <script src="script.js"></script>
</body>

</html>

This is my css:

.navbar {
    background-color: greenyellow;
    width: 100%;
    height: 100px;
}

.sidebar {
    background-color: greenyellow;
    height: 100vh;
    width: 300px;
    left: -100%;
}

.sidebar.active {
    background-color: greenyellow;
    height: 100vh;
    width: 300px;
    left: 0;
}

This is my JS:

const navbar = document.querySelector('.navbar')
const sidebar = document.querySelector('.sidebar')

function active() {
    navbar.addEventListener('click', () => {
        navbar.classList.toggle('active')
        sidebar.classList.toggle('active')
    })
}

Solution

  • 1) You should use addEventListener instead of onClick.

    2) If you want the navbar to slide from left so make it initially at left: -100% and after the button is clicked then make left: 0

    .sidebar {
        background-color: greenyellow;
        height: 100vh;
        width: 300px;
        position: fixed; /* change */
        top: 0; /* change */
        bottom: 0;  /* change */
        left: -100%;    /* change */
    }
    
    .sidebar.active {
        background-color: greenyellow;
        height: 100vh;
        width: 300px;
        left: 0;    /* change */
    

    const sidebar = document.querySelector('.sidebar')
    const faBars = document.querySelector(".fa-bars")
    const faTimes = document.querySelector(".fa-times")
    
    const active = () => sidebar.classList.toggle("active");
    
    faBars.addEventListener("click", active);
    faTimes.addEventListener("click", active);
    .navbar {
      background-color: greenyellow;
      width: 100%;
      height: 100px;
    }
    
    .sidebar {
      background-color: greenyellow;
      height: 100vh;
      width: 300px;
      position: fixed;
      top: 0;
      bottom: 0;
      left: -100%;
    }
    
    .sidebar.active {
      background-color: greenyellow;
      height: 100vh;
      width: 300px;
      left: 0;
    }
    
    .fa-bars,
    .fa-times {
      cursor: pointer;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet"/>
    <div class="navbar">
      <i class="fas fa-bars"></i>
      <div class="sidebar">
        <i class="fas fa-times"></i>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
      </div>
    </div>