Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!