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

128
Visualizações
Animate group of elements and center each element with 1s delay with JavaScript

I need to animate a group of elements across the page (from the right sliding to the left) but instead of this being one smooth animation I want each element to be centered in the container for 1 second before the next element slides in and then the previous element would slide away off screen.

Each element should stay in the center of the container for 1 second before the next one slides over. It's a dynamic set of elements with varied widths and a dynamic number of items. And I want the first element to already be centered which I have already taken care of.

I can't figure out how to animate this with JavaScript so that each element is centered for 1 second at a time before moving on to the next element.

I can't do this with CSS animations/keyframes alone because I don't know how many frames there would be.

window.onload = function() {
  const container = document.querySelector('.tabs');
  const tabs = document.getElementsByClassName('tab');

  // Calculate initial left offset
  const firstTab = tabs[0];
  const initialOffset = (firstTab.offsetWidth / 2);
  container.style.left = 'calc(50% - ' + initialOffset + 'px)';
}
section {
  text-align: center;
  width: 375px;
  background: white;
  margin: 0 auto;
  padding: 16px;
  position: relative;
  display: flex;
  overflow: hidden;
}

.tabs {
  display: flex;
  position: relative;
  margin: 0;
  padding: 0;
  list-style: none;
}

.tab {
  border-radius: 25px;
  background: gray;
  margin: 0 8px;
  text-align: center;
  height: 34px;
  display: inline-flex;
  align-items: center;
  padding: 0 16px;
}
<section>
  <ul class="tabs">
    <li class="tab">Tab One</li>
    <li class="tab">Tab Two</li>
    <li class="tab">Tab Three</li>
  </ul>
</section>

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