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

402
Vistas
How to control when @keyframes is activated?

I wanted to use @keyframes, so I did this:

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  animation: action 5s infinite;
}

@keyframes action {
  0%   {top: 0px;}
  25%  {top: 200px;}
  75%  {top: 50px}
  100% {top: 100px;}
}

However, this code runs when the page loads. Is there a way to trigger the @keyframes at a certain time with js? eg.

for (var i = 0; i < 10; i++) {
    //Do something
}
//Activate @keyframes

Thanks!

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

0

In CSS:

div {
  // your div code
  animation-play-state: paused;
}

In JS:

// do some stuff after the page loads (give your div an ID)
document.getElementById("myDiv").style.animationPlayState = "running"; 
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to trigger the animation using Javascript you just need to set the animation of the element.

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
}

@keyframes action {
  0%   {top: 0px;}
  25%  {top: 200px;}
  75%  {top: 50px}
  100% {top: 100px;}
}
document.getElementByID('your-Elem').style.animation="action 5s infinite";

So in your case, I'd say give the element an ID (or just select all divs if thats what you're going for) and then run this line in your for loop.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can separate animation definition to another css class and trigger it programmatically. Firstly, add class to your div e.g.:

<div class="test">
</div>

Create another css class where you define the animation:

div {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
}

.animate{
   animation: action 5s infinite;
}

@keyframes action {
  0%   {top: 0px;}
  25%  {top: 200px;}
  75%  {top: 50px}
  100% {top: 100px;}
}

Add this class to div element classList when you find it right:

for (let i = 0; i < 10; i++) {
    //
}
document.querySelector('.test').classList.add('animate');
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