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

257
Views
Transition on load works in jQuery; fails in JavaScript

The following setup (see below) animates the div on page load using jQuery, but fails in vanilla JavaScript, in that it gives me the animated state without the animation. I don't wand to use keyframes or a delay, and nothing I tried in JS worked.

Here's the working version, with jQuery:

jQuery(function($) {
  $(document).ready(function() {
    $("#new-selector").addClass("animated-selector");
  });
});
#new-selector {
  background: #3a88fe;
  width: 50px;
  height: 50px;
  transition: transform 500ms ease;
}

#new-selector.animated-selector {
  background: orange;
  transform: translate(75px, 20px) scale(1.5);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="new-selector"></div>

Here's the problematic version, with vanilla JS:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('new-selector').classList.add('animated-selector');
});
#new-selector {
  background: #3a88fe;
  width: 50px;
  height: 50px;
  transition: transform 500ms ease;
}

#new-selector.animated-selector {
  background: orange;
  transform: translate(75px, 20px) scale(1.5);
}
<div id="new-selector"></div>

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

0

For the vanilla JS version you might need to wait for the browser to give you the next animation frame using requestAnimationFrame:

document.addEventListener('DOMContentLoaded', function() {
  requestAnimationFrame(() => document.getElementById('new-selector').classList.add('animated-selector'))
});
#new-selector {
  background: #3a88fe;
  width: 50px;
  height: 50px;
  transition: transform 500ms ease;
}

#new-selector.animated-selector {
  background: orange;
  transform: translate(75px, 20px) scale(1.5);
}
<div id="new-selector"></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!