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

146
Vistas
Update mouse interaction with div after moving it

In a website I'm building, I have a button with a :hover animation, and when you click the button a lot of things on the page including the button move around. My problem is that even after the button moves out from under the mouse, it doesn't update and lose its :hover effect until you move the mouse again.

Here's an example - here once you click the button it stays light blue (the hovered colour) until you move the mouse again.

function clicked() {
  if (document.getElementById('mydiv').style.transform == 'translateY(70px)') {
    document.getElementById('mydiv').style.transform = 'translateY(0px)';
  } else {
    document.getElementById('mydiv').style.transform = 'translateY(70px)';
  }
};
div {
  background-color: #fedcba;
  padding: 20px;
  display: inline-block;
}
div:hover {
  background-color: #abcdef;
}
<div id="mydiv" onclick="clicked();">Click me</div>

How do I make the element update without the user needing to move the mouse again? JQuery is ok.

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

0

I think with your current CSS approach you won't be able to handle that. Try another JavaScript approach:

const el = document.getElementById('mydiv');

el.addEventListener('click', () => {
  if(el.style.transform == 'translateY(50px)') {
    el.style.transform = 'translateY(0px)';
  } else {
    el.style.transform = 'translateY(50px)';
  }
  el.style.background = "#fedcba";
});

el.addEventListener('mouseover', () => {
  el.style.background = "#abcdef";
});

el.addEventListener('mouseout', () => {
  el.style.background = "#fedcba";
});
div {
  background: #fedcba;
  padding: 20px;
  display: inline-block;
}
<div id="mydiv">Click me</div>

If you extract the "hover" into the JS code then you can update the state of the element without the user having to move the mouse.

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