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

194
Vistas
Control CSS keyframes animation progress with React

Let’s say I have a simple keyframes animation to move a box from left to right:

@keyframes mymove {
  0% {
    left: 0px;
  }
  20% {
    left: 200px;
  }
  100% {
    left: 250px;
  }
}

Is there a way to control this animation with a slider ranging from 0-100% in React? Meaning, the animation should always represent the state of the slider. If the slider is moved from 20% to 40% the animation should also be moved from 20% to 40% during that time.

Yes, I know that’s not the intended purpose of keyframes animations but I’d like to find a way regardless.

Here’s a CodeSandbox link: https://codesandbox.io/s/keyframe-experiment-96x5qy

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

0

You can do it with style prop change on box without keyframes

Sandbox

Note that I'm using percentage for calculating left position, if you want to make it move further, you can modify left: progress + "px" with your own calculation

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [progress, setProgress] = useState(0);

  return (
    <div>
      <h1>Progress: {progress}%</h1>
      <div class="box" style={{ left: progress + "px" }}></div>
      <input
        type="range"
        min="0"
        max="100"
        value={progress}
        step="1"
        onInput={(e) => setProgress(e.target.value)}
      />
    </div>
  );
}

The style modification

.box {
  width: 100px;
  height: 100px;
  background: red;
  position: relative;
  transition: left 1s; /*Added transition*/
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

It is difficult to do it the way you want...

In case you only want to stop the keyframe while moving the range slider try this: sanbox

CSS:

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

.animate {
  animation: mymove 5s infinite;
}

App.js:

export default function App() {
  const [progress, setProgress] = useState(0);
  const [moving, setMoving] = useState(false);

  return (
    <div>
      <h1>Progress: {progress}%</h1>
      <p>is moving: {moving ? "true" : "false"}</p>
      <div
        className={`box ${!moving && "animate"}`}
        style={{ left: `${progress}px` }}
      ></div>
      <input
        type="range"
        min="0"
        max="100"
        value={progress}
        step="1"
        onInput={(e) => {
          setMoving(true);
          setProgress(e.target.value);
        }}
        onMouseUp={() => setMoving(false)}
      />
    </div>
  );
}

After the mouseUp event on the range slider, the keyframe animation starts all over again (from the 0%). You can also define keyframes as CSS in JS and change the values depending on the current position of the slider to start the animation from place where the box was moved.

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