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

185
Vistas
Toggle CSS Class Only Works Once

codepen: https://codepen.io/jon424/pen/oNoOQEa

I want to be able to perform this image transition multiple times with several images (there are two in this example). When I click the button, I am able to perform the transition once, and then the toggle (button) won’t work anymore. Any ideas how I can make this button transition work continuously?

HTML

<button id="btn">Toggle</button>
<div id="img1" class="parent">
   <img class="child" src="https://picsum.photos/200/300">
   <div class="cover"></div>
</div>

<div id="img2" class="parent">
   <img class="child" src="https://picsum.photos/200/301">
   <div class="cover"></div>
</div>

CSS

.child {
  width: 300px;
  height: 300px;
}

.parent {
  position: relative;
  width: 300px;
  height: 300px;
}

.cover {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 0;
  width: 100%;
  background: #fff;
  transition: height 1s ease-in-out;
}

.covered {
  height: 100%;
}

.remove_covered {
  top: 0;
  bottom: auto;
  height: 0;
}

JS

const btn = document.querySelector('button'),
    cover = document.querySelectorAll('.cover');

btn.addEventListener('click', ()=> {
  for (i = 0; i < cover.length; i++){
    if(cover[i].classList.contains('covered')){
    cover[i].classList.add('remove_covered');
  } else {
    cover[i].classList.add('covered');
  }

  cover[i].ontransitionend = () => {
    if(cover[i].classList.contains('remove_covered'))
        cover[i].classList.remove('covered','remove_covered');
    };
  }
});
about 4 years ago · Juan Pablo Isaza
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