Search code examples
javascripthtmlcsscss-transitionshamburger-menu

Hamburger not changing to X


I’m trying to create a hamburger button which turns to × when clicked. Everything is working fine, I just can’t make the × sign in a perfect shape. I wanna know how many pixels should I translate the top and bottom bars of the hamburger menus. I checked some tutorials, they do the same thing I did,but for somereason mine is not working.

This is my code:

Thank you for the help in advance.

const navSlide = () => {
  const hamburger = document.querySelector(".hamburger");
  const nav = document.querySelector(".nav-links");
  const navLinks = document.querySelectorAll(".nav-links li");

  hamburger.addEventListener("click", () => {
    //Toggle
    nav.classList.toggle("nav-active");

    //Animate
    navLinks.forEach((link, index) => {
      if (link.style.animation) {
        link.style.animation = "";
      } else {
        link.style.animation = `navLinkFade 0.5s ease forwards ${
          index / 7 + 0.5
        }s`;
      }
    });

    //hamburger animation
    hamburger.classList.toggle("toggle");
  });
};

navSlide();
* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

@import url("https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@1,100&display=swap");
nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 8vh;
  background-color: white;
  font-family: "Roboto Mono", monospace;
}

.logo {
  color: #5b3b97;
  text-transform: uppercase;
  letter-spacing: 0px;
  font-size: 20px;
  font-family: "Times New Roman", Times, serif;
}

.nav-links {
  display: flex;
  justify-content: space-between;
  width: 30%;
}

.nav-links li {
  list-style: none;
  padding-left: 20px;
  padding-right: 5px;
  padding-top: initial;
}

.nav-links a {
  color: #5b3b97;
  text-decoration: none;
  font-weight: bold;
  font-size: 18px;
}

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

.hamburger div {
  width: 25px;
  height: 4px;
  background-color: #5b3b97;
  margin: 5px;
  transition: all 0.3s ease;
}
@media screen and (max-width: 1024px) {
  .nav-links {
    width: 60%;
  }
}

@media screen and (max-width: 768px) {
  body {
    overflow-x: hidden;
  }
  .nav-links {
    position: absolute;
    right: 0px;
    height: 92vh;
    top: 10vh;
    background-color: rgb(255, 255, 255);
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 50%;
    transform: translateX(100%);
    transition: transform 1s ease-in-out;
  }
  .nav-links li {
    opacity: 0;
  }
  .hamburger {
    display: block;
  }
}

.nav-active {
  transform: translateX(0%);
}

@keyframes navLinkFade {
  from {
    opacity: 0;
    transform: translateX(40px);
  }
  to {
    opacity: 1.5;
    transform: translateX(0px);
  }
}

.toggle .line1 {
  transform: rotate(45deg);
  top: 0;
}
.toggle .line2 {
  opacity: 0;
}
.toggle .line3 {
  transform: rotate(-45deg);
  top: 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>Navigation</title>
  </head>
  <body>
    <nav>
      <div class="logo">
        <h4>Bargain Technologies Pvt.Ltd</h4>
      </div>
      <ul class="nav-links">
        <li><a href="#">Home</a></li>
        <li><a href="#">Companies</a></li>
        <li><a href="#">Team</a></li>
        <li><a href="#">Contact us</a></li>
      </ul>
      <div class="hamburger">
        <div class="line 1"></div>
        <div class="line 2"></div>
        <div class="line 3"></div>
      </div>
    </nav>
    <script src="app.js"></script>

    <div>
    </div>
  </body>
</html>


Solution

  • Name in your class="line 1" change to class="line1". There are few things... Declare your .hamburger as position: relative; and give width and height:

      .hamburger {
        position: relative;
        display: none;
        cursor: pointer;
        width: 25px;
        height: 20px;
      }
    

    than, .hamburger div set as position: absolute:

      .hamburger div {
        position: absolute;
        width: 25px;
        height: 4px;
        background-color: #5b3b97;
        transition: all 0.3s ease;
      }
    

    , make correct position for every single line(.hamburger .line1 && .hamburger .line2 && .hamburger .line3):

      .hamburger .line1 {
        top: 0;
      }
    
      .hamburger .line2 {
        top: 50%;
      }
      .hamburger .line3 {
        top: 100%;
      }
    

    finally rotate for 135deg transform: rotate(135deg); and position top for before and after set like top: 50%; and mid line define display: none;:

      .toggle .line1 {
        transform: rotate(-135deg);
        top: 50%;
      }
      .toggle .line2 {
        display: none;
      }
      .toggle .line3 {
        transform: rotate(135deg);
        top: 50%;
      }