Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda