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

136
Views
transition doesn't work editing while window onload

I'm trying to move this square to right 100px after clicking on it with js, I've set the transition to 1s, but it doesn't work

window.onload = function(){
    var square = document.querySelector(".square");        
    square.onclick = function(){
        square.style.right = "100px";
    };
};
.square {
    position: absolute;
    background-color: red;
    width: 500px;
    height: 500px;
    transition: 1s;
}
<div class="square">
</div> 

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

CSS won't animate things from auto to px.
Since right is not defined, it's defaulting to auto.

window.onload = function(){
    var square = document.querySelector(".square");        
    square.onclick = function(){
        square.style.right = "100px";
    };
};
.square {
    position: absolute;
    background-color: red;
    width: 500px;
    height: 500px;
    right: 0;
    transition: 1s;
}
<div class="square">
</div>

However, if you want to animate it without setting right at the beginning, you can also use translate.

window.onload = function(){
    var square = document.querySelector(".square");        
    square.onclick = function(){
        square.style.transform = "translateX(100px)";
    };
};
.square {
    position: absolute;
    background-color: red;
    width: 500px;
    height: 500px;
    transition: 1s;
}
<div class="square">
</div>

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!