Ben Mikola
Ben Mikola

Reputation: 111

Why is my hamburger menu not opening when I click on it?

  setTimeout(() => {
    document.getElementById('me').classList.add('fade');
  }, 2000); 
*{
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.menu-wrap{
  position: fixed;
  top: 0;
  right: 0;
  z-index: 1;
}

.toggler{
  position: absolute;
  top: 0;
  right: 0;
  z-index: 2;
  cursor: pointer;
  height: 50px;
  width: 50px;
  opacity: 0;
}

.hamburger{
  position: absolute;
  top: 0;
  right: 0;
  z-index: 1;
  width: 60px;
  height: 60px;
  padding: 1rem;
  background: #1a1a1a;
  display: flex;
  align-items: center;
  justify-content: center;
}

/*hamburger line*/

.hamburger > div{
  position: relative;
  width: 100%;
  height: 2px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.4s ease;
}

.hamburger > div:before,
.hamburger > div:after{
  content: '';
  position: absolute;
  z-index: 1;
  top: -10px;
  width: 100%;
  height: 2px;
  background: inherit;
}

.hamburger > div:after{
  top: 10px;
}

/*show when clicked*/
.menu-wrap .toggler:checked ~ .menu{
  visibility: visible;
}

.menu-wrap .toggler:checked ~ .menu > div{
  transform: scale(1);
  transition-duration: 0.7s;
}

.menu-wrap .toggler:checked ~ .menu > div > div{
  opacity: 1;
  transition: 0.4s ease;
}

.toggler:checked + .hamburger > div{
  transform: rotate(135deg);
}

/* turn lines into x*/
.toggler:checked + .hamburger > div:before,
.toggler:checked + .hamburger > div:after{
  top: 0;
  transform: rotate(90deg);
}

/*rotate on hover when checked*/
.toggler:checked:hover + .hamburger > div{
  transform: rotate(225deg);
}

.menu-wrap .menu{
  position: fixed;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  visibility: hidden;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.menu > div{
  background: #000;
  border-radius: 50%;
  width: 200vw;
  height: 200vw;
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  transform: scale(0);
  transition: all 0.4s ease;
}

.menu > div > div{
  text-align: center;
  max-width: 90vw;
  max-height: 100vh;
  opacity: 0;
}

.menu > div > div > ul >li{
  list-style: none;
  padding: 1rem;
  color: #fff;
}

.menu > div > div > ul > li > a{
  text-decoration: none;
  transition: 0.4s ease;
  color: #fff;
}


body{
  background-color: #1a1a1a;
  color: #fff;
  font-family: sans-serif;
  font-size: 1.3rem;
  line-height: 2rem;
}

#first{
  font-size: 5rem;
}

#me{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  background-image: url("images/mecrop2.jpg");
  z-index: 3;
  display:flex;
  align-items: center;
  justify-content: center;
  transition: opacity 2.5s;
}

.fade#me{
  opacity: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" href="mywebsite.css">
</head>
<body>
  <div id="me">
    <h1 id="first">Hi! I'm Ben.</h1>
  </div>
  <header>
    <div class="name">
      <h1>Ben Mikola</h1>
    </div>
  </header>

  <div class="menu-wrap">
    <input type="checkbox" class="toggler">
    <div class="hamburger">
      <div></div>
    </div>
    <div class="menu">
      <div>
        <div>
          <ul>
            <li><a href="#">What I'm Doing Now</a></li>
            <li><a href="#">Dating/Relationships</a></li>
            <li><a href="#">Programming</a></li>
            <li><a href="#">Contact Me!</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

The hamburger menu only works if the first div with the id of "me" is not there. Why does that div interfere with the menu's functionality? I tried changing the z-index of the div. I moved it to the bottom of my HTML. I think it might be the JavaScript messing with it, but I don't know what to change since I don't know much Javascript.

Upvotes: 0

Views: 139

Answers (2)

Always Learning
Always Learning

Reputation: 5581

The #me div is overlapping the hamburger menu and stops clicks from reaching the menu. Its width is 100% and its z-index is 3. The simplest way to fix it is to set the z-index for #me to be -1 so that it's behind everything on the page.

Upvotes: 2

A. Meshu
A. Meshu

Reputation: 4148

#me have higher z-index then .menu-wrap. So without changing your markup you can simply do: .menu-wrap: z-index: 9; and that's it.

Upvotes: 4

Related Questions