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

217
Vistas
add EventListener to element under element

I have two images on top of each other.

I would like to add a mouseenter and mouseleave EventListener to the image on the bottom and ​I would like for the EventListeners to completely ignore the image on top.

How can I do this?


This is my code: (Fiddle here: https://jsfiddle.net/tLyk6pxu/)

document.querySelector(".testImageBottom").addEventListener('mouseenter', () => {
    console.log("trigger: mouseenter");
});
    
document.querySelector(".testImageBottom").addEventListener('mouseleave', () => {
    console.log("trigger: mouseleave");
});
img {
  position: absolute;
}

.testImageBottom {
  width: 400px;
}

.testImageTop {
  width: 200px;
  opacity: .5;
}
<img class="testImageBottom" src="https://images.unsplash.com/photo-1527549993586-dff825b37782?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2370&q=80" alt="test">

<img class="testImageTop" src="https://images.unsplash.com/photo-1472396961693-142e6e269027?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=952&q=80" alt="test">


As you can see, there are two images on top of each other. – The bottom image has EventListeners for mouseenter and mouseleave and I need these EventListeners to only look at the edges of the bottom-image and ignore the top-image.

I guess I could just create an empty div that has exactly the same size and position as the bottom image and then position that div over everything and add the EventListeners to the empty div. – But I'm hoping that there's a more elegant solution? ;)

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

0

Just add CSS pointer-events:none to your imageTop

document.querySelector(".testImageBottom").addEventListener('mouseenter', () => {
    console.log("trigger: mouseenter");
});
    
document.querySelector(".testImageBottom").addEventListener('mouseleave', () => {
    console.log("trigger: mouseleave");
});
img {
  position: absolute;
}

.testImageBottom {
  width: 400px;
}

.testImageTop {
  width: 200px;
  opacity: .5;
  pointer-events:none;
}
<img class="testImageBottom" src="https://images.unsplash.com/photo-1527549993586-dff825b37782?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2370&q=80" alt="test">

<img class="testImageTop" src="https://images.unsplash.com/photo-1472396961693-142e6e269027?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=952&q=80" alt="test">

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