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

246
Views
How to make a carousel to change slides after 3 items, javascript

I am trying to create a carousel with JavaScript for a WordPress website and i have made it work by showing 1 slide at a time with this code:

const slides = document.querySelectorAll(".slider-slide");

const prev = document.querySelector(".slider-arrows-left");
const next = document.querySelector(".slider-arrows-right");

let slideIndex = 0;
showSlide(slideIndex);

function showSlide() {
  if (slideIndex > slides.length - 1) {
    slideIndex = 0;
  }

  if (slideIndex < 0) {
    slideIndex = slides.length - 1;
  }

  let i;
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  slides[slideIndex].style.display = "block";
}

next.addEventListener("click", () => {
  showSlide((slideIndex += 1));
});

prev.addEventListener("click", () => {
  showSlide((slideIndex -= 1));
});

My question is how can i make the same carousel to constantly displays 3 slides and by pressing 'next' the first slide to become display="none" and the fourth to be display="block"?

And then, by pressing 'next' again, the second slide to become display="none" and the fifth to be display="block", then the third slide to become display="none" and the sixth to be display="block" e.t.c. until the end of the array?

Also to do it to go back with the 'prev' button?

I think its pretty simple, but i don't know how exactly to write the code... i am pretty new in coding..

Thank you

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!