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

175
Views
Trying to create a js slider by changing z-index of images

in js I have an object containing 3 images

HTMLCollection(3) [img.lineup-image-1.images, img.lineup-image-2.images, img.lineup-image-3.images]

and an anchor tag with an onclick function that is supposed to work as a "next" button in a slideshow.

function next(){
    for (i = 0; i < slides.length; i++) {

        if (i == slides.length - 1) {
          console.log(i);
          slides[0].style.zIndex = "20";
          slides[i].style.zIndex = "10";
        }
      }

      for (i = 0; i < slides.length; i++) {
          if (i !== slides.length - 1) {
            slides[i].style.zIndex = "10";
            slides[i + 1].style.zIndex = "20";
            console.log("rokoko");
          }

          }
      }

Logic I'm trying to write :

3 images are position absolutely on top of each other with the last one showing on top.

First all elements in the object are assigned z-index of 10. After that I assign the last element z-index of 20.

If the button is clicked and the current slide is the last indexed in the object -> change current slide's z-index to 10 and change the first image's z-index to 20.

Else --> give the current slide z-index of 10 and give the next slide z-index of 20.

I feel like I'm getting lost in the nuances of how for loop works, I know where the problem is (first for loop checks for the last object and when true changes the z-indexes, but the second loop goes through the object items right after that and changes z-indexes at once), but how to build it so that the program can also check if the "current image" is not the last and also change the z-indexes accordingly? Thanks

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!