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

275
Vistas
¿Por qué la animación CSS a través de javascript solo ocurre una vez?

Estoy trabajando en hacer una barra de navegación, porque es entretenida, pero cuando trato de hacer un ligero desvanecimiento al pasar el mouse usando animaciones, por alguna razón, el hoverout implementado en javascript solo funciona una vez, y nunca lo vuelve a hacer hasta que se actualice.

¿Por qué es eso, y hay alguna alternativa para hacer esto en javascript?

 let navChildren = [...document.getElementById('nav-container').children] navChildren.forEach(x => { x.addEventListener('click', () => { }) x.addEventListener('mouseout', () => { x.style.animation = 'hoverOut 1s forwards' }) })
 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { animation: hoverIn 0.5s forwards; } @keyframes hoverIn { 0% { background-color: #333; } 100% { background-color: #111; } } @keyframes hoverOut { 0% { background-color: #111; } 100% { background-color: #333; } } .active { background-color: #222; }
 <html> <body> <ul id = 'nav-container'> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Debe eliminar la animación y luego volver a configurarla para restablecerla:

 let navChildren = [...document.getElementById('nav-container').children] navChildren.forEach(x => { x.addEventListener('click', () => { }) x.addEventListener('mouseout', () => { x.style.animation = "none"; setTimeout(function() { x.style.animation = 'hoverOut 1s forwards' }, 0) }) })
 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { animation: hoverIn 0.5s forwards; } @keyframes hoverIn { 0% { background-color: #333; } 100% { background-color: #111; } } @keyframes hoverOut { 0% { background-color: white; } 100% { background-color: #333; } } .active { background-color: #222; }
 <html> <body> <ul id='nav-container'> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>

Aunque una forma más fácil es simplemente usar transiciones CSS, no se requiere JavaScript:

 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; transition: 0.5s; } li a:hover { background-color: white; } @keyframes hoverOut { 0% { background-color: white; } 100% { background-color: #333; } } .active { background-color: #222; }
 <html> <body> <ul id='nav-container'> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>

over 4 years ago · Santiago Trujillo Denunciar

0

Agregue el detector de eventos animationend para actualizar la animación.

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Animations/Using_CSS_animations#using_animation_events

 let navChildren = [...document.getElementById('nav-container').children] navChildren.forEach(x => { x.addEventListener("animationend",()=>x.style.animation = "none"); x.addEventListener('mouseout',()=>x.style.animation = 'hoverOut 1s forwards'); })
 ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover { animation: hoverIn 0.5s forwards; } @keyframes hoverIn { 0% { background-color: #333; } 100% { background-color: #111; } } @keyframes hoverOut { 0% { background-color: #111; } 100% { background-color: #333; } } .active { background-color: #222; }
 <html> <body> <ul id = 'nav-container'> <li><a href="#home">Home</a></li> <li><a href="#news">News</a></li> <li><a href="#contact">Contact</a></li> <li><a href="#about">About</a></li> </ul> </body> </html>

over 4 years ago · Santiago Trujillo 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