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

265
Visualizações
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 Respostas
Responde à pergunta

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 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