Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
How do I get the position in the middle of a CSS Animation?

So I'm making a music website with a playhead that I've animated to move along the screen and loop back.

.playhead {
    position: absolute;
    border-left: 0.2em solid;
    min-height: calc(100% - var(--display-border-width));
    border-color: red;
    animation: play-animation 4s linear infinite;
}

@keyframes play-animation {
    0% {
        left: 0%;
    }
    100% {
        left: 100%
    }
}

Now while the playhead is moving, I want to access its position value, or how far along the screen it is (offsetX), so I can play the notes or whatever at that position.

So if it halfway across the screen, and the screen is 1000px wide, I want to access the 500px value during the animation.

I've tried using useEffect and useRef together, but that only gets called when the animation stops or starts, but not while it is running. So the value stays the same during the animation

I've tried setting up a seperate setInterval for calculating the position only, like so:

useEffect(() => {
        if (props.play) {
            const markers = (playTime * 1000) / frameRate;
            const percentToAdd = 100 / markers;

            const id = setInterval(() => {setProgress((prev) => (prev + percentToAdd) % 100)}, frameRate);
            setIntervalID(id);
        } else {
            if (intervalID !== 0) {
                clearInterval(intervalID);
            }
        }
    }, [props.play]);

But this is out of sync for some reason. And plus, I'd rather the position values come from the playhead directly.

Is there any way to do this? Thanks!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda