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

137
Visualizações
Javascript request animation frame irregular translation

SO community! I'm trying to make an object move at the same phase as scroll using CSS transform. The problem is that I see the animation is not smooth. Instead, is like the box is flickering and 'jumping' all the time, and I can't figure out the cause.

How can I achieve these kinds of transformations in a smooth way?

I will leave my code below, as well as a codepen link.

Thanks beforehand for your help!

const container = document.querySelector('.container');
const box = document.querySelector('.box');

function animate() {
  transformBox();
  requestAnimationFrame(animate);
}

function transformBox() {
  const scrollY = window.scrollY;
  box.style.transform = `translate3d(0, ${scrollY}px, 0)`;
}

animate();
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  overscroll-behavior: none;
}

.container {
  position: relative;
  height: 400vh;
  background-color: #333;
}

.box {
  position: absolute;
  top: calc(50vh - 25px);
  left: calc(50% - 25px);
  width: 50px;
  height: 50px;
  background-color: white;
  border-radius: 8px;
}
<div class="container">
  <div class="box"></div>
</div>

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