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

142
Visualizações
How to wait for the end of the element transform property?

In this example, after the button is clicked, I first want to transform the box and then show an alert. But here, it first show the alert and then do the transform

const clickme = document.getElementById('clickme');
const box = document.getElementById('box');

clickme.addEventListener("click", () => {
        box.style.transform = `rotate(90deg)`;
    const value = parseFloat(box.style.transform.split('rotate(')[1]);
    if(value >= 90) {
        alert('Hello Rotate')
    }
})
#box {
  width: 100px;
  height: 100px;
  background: tomato;
  transition: .200s ease;
}
<div id="box">
  
</div>

<button id="clickme">
  rotate
</button>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

That's the limit for transform for you. To achieve best performance, a reliable result and actually use the right tools for the right purpose i'd suggest learning CSS animations with keyframes. It's not that hard and it will allow you to unload some of the tasks from the main thread to the gpu(better SEO and performance). Else there are plenty of libraries that you could use:

For example: https://animejs.com/

about 4 years ago · Juan Pablo Isaza Relatório

0

In your particular case, the problem is not of the transform property or not having keyframes as Filas' answer pointed out, but instead the problem lies in the nature of Javascript.

const clickme = document.getElementById('clickme');
const box = document.getElementById('box');

clickme.addEventListener("click", () => {
        box.style.transform = `rotate(90deg)`;
    const value = parseFloat(box.style.transform.split('rotate(')[1]);
    if(value >= 90) {
        alert('Hello Rotate')
    }
})

The browser pauses the rendering of the HTML, which is responsible for box.style.transform = 'rotate(90deg)';, and instead prioritizes showing the alert(). Here's a thread that talks about it more.

I assume that in your actual code, you would be using some other function, instead of the alert(). In that case, your code should run as you intended, with the transform occurring first, followed by the subsequent commands.

const clickme = document.getElementById('clickme');
const box = document.getElementById('box');

clickme.addEventListener("click", () => {
        box.style.transform = 'rotate(90deg)';
    const value = parseFloat(box.style.transform.split('rotate(')[1]);
    if(value >= 90) {
        box.style.backgroundColor="green";  //code that isn't an alert()
    }
})
#box {
  width: 100px;
  height: 100px;
  background: tomato;
  transition: .200s ease;
}
<div id="box">
  
</div>

<button id="clickme">
  rotate
</button>

Combine this with a transitionend event, or a setTimeout() function, if you want to sequence the changes in a way that the individual changes/steps are clearly visible to the user.

about 4 years ago · Juan Pablo Isaza Relatório
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