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