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

447
Views
Javascript: Horizontal animation of divs is not smooth

I have a simple Javascript script that may automatically scroll through all divs inside of "ReviewsWrapper" and on the end maximally smoothly start again this animation from the beginning.

Now, this animation is lagging thought whole animation

Is there any option for how I can make this animation as I mentioned at the beginning?

const flavoursContainer = document.getElementById('ReviewsWrapper');
const flavoursScrollWidth = flavoursContainer.scrollWidth;


window.addEventListener('load', () => {
  self.setInterval(() => {
    const first = document.querySelector('#ReviewsWrapper div');

    if (!isElementInViewport(first)) {
      flavoursContainer.appendChild(first);
      flavoursContainer.scrollTo(flavoursContainer.scrollLeft - first.offsetWidth, 0);
    }
    if (flavoursContainer.scrollLeft !== flavoursScrollWidth) {
      flavoursContainer.scrollTo(flavoursContainer.scrollLeft + 1, 0);
    }
  }, 5);
});

function isElementInViewport(el) {
  var rect = el.getBoundingClientRect();
  return rect.right > 0;
}
#ReviewsWrapper {
  position: relative;
  max-width: 100vw;
  overflow-y: hidden;
  overflow-x: scroll;
  -ms-overflow-style: none;
  scrollbar-width: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.review {
  min-width: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  margin: 8px;
  background: yellow;
}

p {
  margin-left: 8px;
}
<div id="ReviewsWrapper">
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review ONE
    </p>
  </div>
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review TWO
    </p>
  </div>
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review THREE
    </p>
  </div>
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review FOUR
    </p>
  </div>
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review FIVE
    </p>
  </div>
  <div class="review">
    <img src="https://via.placeholder.com/150">
    <p>Review SIX
    </p>
  </div>
</div>

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

0

I was searching what was creating this "jump".

It's the margin of the review class. The issue with removing margin is the bad view. I just edit it to make it as you show.

Before:

<div class="review"><!-- with margin -->
   <!-- all content -->
</div>

After:

<div class="review"><!-- without margin -->
   <div class="sub-review"><!-- with padding -->
      <!-- all content -->
   </div>
</div>

Here an example, without margin and without jump :

const flavoursContainer = document.getElementById('ReviewsWrapper');
const flavoursScrollWidth = flavoursContainer.scrollWidth;


window.addEventListener('load', () => {
  self.setInterval(() => {
    const first = document.querySelector('#ReviewsWrapper div');

    if (!isElementInViewport(first)) {
      flavoursContainer.appendChild(first);
      flavoursContainer.scrollTo(flavoursContainer.scrollLeft - first.offsetWidth, 0);
    }
    if (flavoursContainer.scrollLeft !== flavoursScrollWidth) {
      flavoursContainer.scrollTo(flavoursContainer.scrollLeft + 1, 0);
    }
  }, 5);
});

function isElementInViewport(el) {
  var rect = el.getBoundingClientRect();
  return rect.right > 0;
}
#ReviewsWrapper {
  position: relative;
  max-width: 100vw;
  overflow-y: hidden;
  overflow-x: scroll;
  -ms-overflow-style: none;
  scrollbar-width: none;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.review {
  min-width: 300px;
  padding: 8px;
}

.sub-review {
  padding: 8px 0px 8px 0px;
  align-items: center;
  justify-content: center;
  display: flex;
  width: 100%;
  background: yellow;
}

p {
  margin-left: 8px;
}
<div id="ReviewsWrapper">
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review ONE</p>
    </div>
  </div>
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review TWO</p>
    </div>
  </div>
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review THREE</p>
    </div>
  </div>
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review FOUR</p>
    </div>
  </div>
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review FIVE</p>
    </div>
  </div>
  <div class="review">
    <div class="sub-review">
      <img src="https://via.placeholder.com/150">
      <p>Review SIX</p>
    </div>
  </div>
</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!