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

355
Vistas
Trying to use transitionend in a separate js file

I am trying to use transitionend based on a tutorial I'm following. In the tutorial, transitionend is used in the same file as the html, i.e., it is in a tag at the end of the body element. It removes the 'playing' class from the elements.

I decided to move all the JS to a separate js file, which in this case has been named the generic 'script.js'.

The rest of the JS code seems to work perfectly except the key.addEventListener that uses the transitionend event. Here's what it looks like:

function removeTransition(e) {
  if (e.propertyName !== 'transform') return;
  e.target.classList.remove('playing');
}

function playSound(e) {
  const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
  const key = document.querySelector(`div[data-key="${e.keyCode}"]`);
  if (!audio) return;

  key.classList.add('playing');
  audio.currentTime = 0;
  audio.play();
}

const keys = Array.from(document.querySelectorAll('.key'));
keys.forEach(key => key.addEventListener('transitionend', removeTransition));
window.addEventListener('keydown', playSound)

If I keep the JS code in the html file, the transitionend event works just fine and removes the 'playing' class. However, if I move it to the script.js file, transtionend never triggers. I'm assuming this is related to the documentation stating:

"In the case where a transition is removed before completion, such as if the transition-property is removed or display is set to none, then the event will not be generated."

But I don't know in what way that's happening here, and, more importantly, how to work around it in order to use transitionend in a separate .js file.

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