Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
Javascript eventListener only working in some areas of the element

So i was building a burger menu responsive navbar. I finished up most of the working but i got stuck at one point. The problem is i have applied the javascript onClick event listener on the div .burger so that it changes the opacity of the .nav-container to 1. Up until to this point this works fine. The problem is this click event only works in some areas on the .burger. Like if clicked on of the lines in it or somewhere to the right. I can't seem to find the problem. Can someone help me with this? Thanks in advance.

Here is my code

function showNav() {
  var nav = document.getElementById("nav");

  nav.classList.add("show-nav");
}
* {
  margin: 0px;
  padding: 0;
  box-sizing: border;
}

.container {
  display: flex;
  margin-top: 2%;
}

header {
  position: relative;
  z-index: 1;
}

.line1,
.line2,
.line3 {
  width: 40px;
  height: 8px;
  background-color: black;
  margin-top: 3px;
}

.nav-heading {
  flex: 4;
  margin-left: 5%;
}

.burger {
  flex: 1;
  margin-left: 40%;
  background-color: red;
  padding: 0;
  width: auto;
}

.nav-container {
  position: absolute;
  width: 100%;
  opacity: 0;
  transition: .2s ease-in-out;
  transform: translateY(-20%);
  transform-origin: left;
  ul {
    display: flex;
    flex-direction: column;
    background-color: blue;
    width: 100%;
    align-items: center;
    list-style-type: none;
    li {
      flex: 1;
      padding: 15px;
    }
  }
}

.show-nav {
  opacity: 1;
  transform: translateY(20%);
}
<header>
  <div class="container">
    <div class="nav-heading">
      <h1>Navbar</h1>
    </div>

    <div class="burger" onclick="showNav()">

      <div class="line1"></div>
      <div class="line2"></div>
      <div class="line3"></div>

    </div>

  </div>


  <div class="nav-container" id="nav">
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact</a></li>

    </ul>
  </div>
</header>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just put z-index = 1 on .burger.

.burger {
  flex: 1;
  margin-left: 40%;
  background-color: red;
  padding: 0;
  width: auto;
  z-index: 1;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

to bring your menu up front , you can add position:relative + z-index:1; to make sure it stands on top.

You also add the rule cursor: pointer; to show it is a clickable element :)

For the class you add, you could use classList . toggle() , so it shows/hides alternatively.

example

function showNav() {
  var nav = document.getElementById("nav");

  nav.classList.toggle("show-nav");
}
* {
  margin: 0px;
  padding: 0;
  box-sizing: border;
}

.container {
  display: flex;
  margin-top: 2%;
}

header {
  position: relative;
  z-index: 1;
}

.line1,
.line2,
.line3 {
  width: 40px;
  height: 8px;
  background-color: black;
  margin-top: 3px;
}

.nav-heading {
  flex: 4;
  margin-left: 5%;
}

.burger {
  flex: 1;
  margin-left: 40%;
  background-color: red;
  padding: 0;
  width: auto;
  cursor: pointer;
  position:relative;
  z-index:1
}

.nav-container {
  position: absolute;
  width: 100%;
  opacity: 0;
  transition: .2s ease-in-out;
  transform: translateY(-20%);
  transform-origin: left;
}

ul {
  display: flex;
  flex-direction: column;
  background-color: blue;
  width: 100%;
  align-items: center;
  list-style-type: none;
}

li {
  flex: 1;
  padding: 15px;
}

li a {
  color: black;
}

.show-nav {
  opacity: 1;
  transform: translateY(20%);
}
<header>
  <div class="container">
    <div class="nav-heading">
      <h1>Navbar</h1>
    </div>

    <div class="burger" onclick="showNav()">

      <div class="line1"></div>
      <div class="line2"></div>
      <div class="line3"></div>

    </div>

  </div>


  <div class="nav-container" id="nav">
    <ul>
      <li><a href="">Home</a></li>
      <li><a href="">About</a></li>
      <li><a href="">Contact</a></li>

    </ul>
  </div>
</header>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just change the width of

.nav-container {
  width: 30%;
}

so it doesn't overlap the burger. In general your CSS structure is not good.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda