Home > Software engineering >  How can I adjust the 3-D Image Slider Carousel to transition between slides correctly?
How can I adjust the 3-D Image Slider Carousel to transition between slides correctly?

Time:11-24

Using the code from https://codepen.io/fghty/pen/JjJZreX, I am trying to add 3 more slides to the 5 slide HTML 3-D Image Slider Carousel.

I have added the images and then mapped the slides CSS, however it does not transition correctly between slides as in the 5 slide example, with the first checked image displaying for the first 6 of the slides.

How can I adjust my code so the 8 slides transition as per the 5 slide example?

<html>

            <style media="screen">
                body {
              margin: 0;
              background: #EEE;
              user-select: none;
              font-family: sans-serif;
            }


            #slider {
              position: relative;
              width: 50%;
              height: 32vw;
              margin: 150px auto;
              font-family: 'Helvetica Neue', sans-serif;
              perspective: 1400px;
              transform-style: preserve-3d;
            }

            input[type=radio] {
              position: relative;
              top: 108%;
              left: 50%;
              width: 18px;
              height: 18px;
              margin: 0 15px 0 0;
              opacity: 0.4;
              transform: translateX(-83px);
              cursor: pointer;
            }


            input[type=radio]:nth-child(8) {
              margin-right: 0px;
            }

            input[type=radio]:checked {
              opacity: 1;
            }




            #slider label,
            #slider label img {
              position: absolute;
              width: 100%;
              height: 100%;
              left: 0;
              top: 0;
              color: white;
              font-size: 70px;
              font-weight: bold;
              border-radius: 3px;
              cursor: pointer;
              display: flex;
              align-items: center;
              justify-content: center;
              transition: transform 400ms ease;
            }



            /* Slider Functionality */

            /* Active Slide */
            #s1:checked ~ #slide1,
             #s2:checked ~ #slide2,
              #s3:checked ~ #slide3,
               #s4:checked ~ #slide4,
                #s5:checked ~ #slide5,
                /* add new slides*/
                 #s6:checked ~ #slide6,
               #s7:checked ~ #slide7,
                #s8:checked ~ #slide8 {
              box-shadow: 0 13px 26px rgba(0,0,0, 0.3), 0 12px 6px rgba(0,0,0, 0.2);
              transform: translate3d(0%, 0, 0px);
            }

            /* Next Slide */
            #s1:checked ~ #slide2,
             #s2:checked ~ #slide3,
              #s3:checked ~ #slide4,
               #s4:checked ~ #slide5,
                #s5:checked ~ #slide6,
                /* add new slides*/
                #s6:checked ~ #slide7,
               #s7:checked ~ #slide8,
                #s8:checked ~ #slide1{
              box-shadow: 0 6px 10px rgba(0,0,0, 0.3), 0 2px 2px rgba(0,0,0, 0.2);
              transform: translate3d(20%, 0, -100px);
            }


            /* Next to Next Slide */
            #s1:checked ~ #slide3,
             #s2:checked ~ #slide4,
              #s3:checked ~ #slide5,
               #s4:checked ~ #slide1,
                #s5:checked ~ #slide2,
                /* add new slides*/
                 #s6:checked ~ #slide3,
               #s7:checked ~ #slide4,
                #s8:checked ~ #slide5
                
                {
              box-shadow: 0 1px 4px rgba(0,0,0, 0.4);
              transform: translate3d(40%, 0, -250px);
            }

            /* Previous to Previous Slide */
            #s1:checked ~ #slide4,
             #s2:checked ~ #slide5,
              #s3:checked ~ #slide1,
               #s4:checked ~ #slide2,
                #s5:checked ~ #slide3,
                /* add new slides*/
                 #s6:checked ~ #slide4,
               #s7:checked ~ #slide5,
                #s8:checked ~ #slide6
                {
              box-shadow: 0 1px 4px rgba(0,0,0, 0.4);
              transform: translate3d(-40%, 0, -250px);
            }

            /* Previous Slide */
            #s1:checked ~ #slide5,
             #s2:checked ~ #slide1,
              #s3:checked ~ #slide2,
               #s4:checked ~ #slide3,
                #s5:checked ~ #slide4,
                /* add new slides*/
                #s6:checked ~ #slide5,
               #s7:checked ~ #slide6,
                #s8:checked ~ #slide7{
              box-shadow: 0 6px 10px rgba(0,0,0, 0.3), 0 2px 2px rgba(0,0,0, 0.2);
              transform: translate3d(-20%, 0, -100px);
            }


              </style>
            </head>
            <body>
            <section id="slider">
              <input type="radio" name="slider" id="s1" checked>
              <input type="radio" name="slider" id="s2">
              <input type="radio" name="slider" id="s3">
              <input type="radio" name="slider" id="s4">
              <input type="radio" name="slider" id="s5">
              <input type="radio" name="slider" id="s6">
              <input type="radio" name="slider" id="s7">
               <input type="radio" name="slider" id="s8">
             

              <label for="s1" id="slide1"><img src="https://img.freepik.com/free-photo/empty-sea-beach-background_74190-313.jpg?size=626&ext=jpg" alt=""></label>
              <label for="s2" id="slide2"><img src="https://t4.ftcdn.net/jpg/03/09/56/17/360_F_309561741_65AVmSUj9PuybfZFo2McgkOw6jTxfCtc.jpg" alt=""></label>
              <label for="s3" id="slide3"><img src="https://thumbs.dreamstime.com/b/sunrise-over-beach-cancun-beautiful-mexico-40065727.jpg" alt=""></label>
              <label for="s4" id="slide4"><img src="https://media.istockphoto.com/photos/couple-relax-on-the-beach-enjoy-beautiful-sea-on-the-tropical-island-picture-id1160947136?k=20&m=1160947136&s=612x612&w=0&h=TdExAS2--H3tHQv2tc5woAl7e0zioUVB5dbIz6At0I4=" alt=""></label>
              <label for="s5" id="slide5"><img src="https://image.shutterstock.com/image-photo/chairs-umbrella-palm-beach-tropical-260nw-559599520.jpg" alt=""></label>
               <label for="s3" id="slide6"><img src="https://media.istockphoto.com/photos/tropical-beach-in-punta-cana-dominican-republic-caribbean-island-picture-id1150862958?b=1&k=20&m=1150862958&s=170667a&w=0&h=Fp6mNIh4pWJBBnm4dkpQfi-rjU-9D-HzEAV5Zgyc6wE=" alt=""></label>
              <label for="s4" id="slide7"><img src="https://cdn.pixabay.com/photo/2016/10/18/21/22/beach-1751455__480.jpg" alt=""></label>
              <label for="s5" id="slide8"><img src="https://img.freepik.com/free-photo/beautiful-tropical-empty-beach-sea-ocean-with-white-cloud-blue-sky-background_74190-13665.jpg?size=626&ext=jpg" alt=""></label>
            </section>


</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Here is the working example for 5 slides for comparison:

    <style media="screen">
    body {
  margin: 0;
  background: #EEE;
  user-select: none;
  font-family: sans-serif;
}


#slider {
  position: relative;
  width: 50%;
  height: 32vw;
  margin: 150px auto;
  font-family: 'Helvetica Neue', sans-serif;
  perspective: 1400px;
  transform-style: preserve-3d;
}

input[type=radio] {
  position: relative;
  top: 108%;
  left: 50%;
  width: 18px;
  height: 18px;
  margin: 0 15px 0 0;
  opacity: 0.4;
  transform: translateX(-83px);
  cursor: pointer;
}


input[type=radio]:nth-child(5) {
  margin-right: 0px;
}

input[type=radio]:checked {
  opacity: 1;
}




#slider label,
#slider label img {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  color: white;
  font-size: 70px;
  font-weight: bold;
  border-radius: 3px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 400ms ease;
}



/* Slider Functionality */

/* Active Slide */
#s1:checked ~ #slide1,
 #s2:checked ~ #slide2,
  #s3:checked ~ #slide3,
   #s4:checked ~ #slide4,
    #s5:checked ~ #slide5 {
  box-shadow: 0 13px 26px rgba(0,0,0, 0.3), 0 12px 6px rgba(0,0,0, 0.2);
  transform: translate3d(0%, 0, 0px);
}

/* Next Slide */
#s1:checked ~ #slide2,
 #s2:checked ~ #slide3,
  #s3:checked ~ #slide4,
   #s4:checked ~ #slide5,
    #s5:checked ~ #slide1 {
  box-shadow: 0 6px 10px rgba(0,0,0, 0.3), 0 2px 2px rgba(0,0,0, 0.2);
  transform: translate3d(20%, 0, -100px);
}


/* Next to Next Slide */
#s1:checked ~ #slide3,
 #s2:checked ~ #slide4,
  #s3:checked ~ #slide5,
   #s4:checked ~ #slide1,
    #s5:checked ~ #slide2 {
  box-shadow: 0 1px 4px rgba(0,0,0, 0.4);
  transform: translate3d(40%, 0, -250px);
}

/* Previous to Previous Slide */
#s1:checked ~ #slide4,
 #s2:checked ~ #slide5,
  #s3:checked ~ #slide1,
   #s4:checked ~ #slide2,
    #s5:checked ~ #slide3 {
  box-shadow: 0 1px 4px rgba(0,0,0, 0.4);
  transform: translate3d(-40%, 0, -250px);
}

/* Previous Slide */
#s1:checked ~ #slide5,
 #s2:checked ~ #slide1,
  #s3:checked ~ #slide2,
   #s4:checked ~ #slide3,
    #s5:checked ~ #slide4 {
  box-shadow: 0 6px 10px rgba(0,0,0, 0.3), 0 2px 2px rgba(0,0,0, 0.2);
  transform: translate3d(-20%, 0, -100px);
}


  </style>
</head>
<body>
<section id="slider">
  <input type="radio" name="slider" id="s1" checked>
  <input type="radio" name="slider" id="s2">
  <input type="radio" name="slider" id="s3">
  <input type="radio" name="slider" id="s4">
  <input type="radio" name="slider" id="s5">

  <label for="s1" id="slide1"><img src="https://i.pinimg.com/originals/2b/de/de/2bdede0647e3cdf75b44ea33723201d9.jpg" alt=""></label>
  <label for="s2" id="slide2"><img src="https://images6.alphacoders.com/462/thumb-1920-462371.jpg" alt=""></label>
  <label for="s3" id="slide3"><img src="https://wallpaperaccess.com/full/1154341.jpg" alt=""></label>
  <label for="s4" id="slide4"><img src="https://wallpapercave.com/wp/wp2634222.jpg" alt=""></label>
  <label for="s5" id="slide5"><img src="https://images5.alphacoders.com/343/thumb-1920-343645.jpg" alt=""></label>
</section>

CodePudding user response:

You have wrong id's, wrong tranlate3d values, and you forgot to add additional checked id's to account for your 3 additional slides. The snippet below should now work.

<html>

  <style media="screen">
    body {
      margin: 0;
      background: #EEE;
      user-select: none;
      font-family: sans-serif;
    }

    <style media="screen">body {
      margin: 0;
      background: #EEE;
      user-select: none;
      font-family: sans-serif;
    }

    #slider {
      position: relative;
      width: 50%;
      height: 32vw;
      margin: 150px auto;
      font-family: 'Helvetica Neue', sans-serif;
      perspective: 1400px;
      transform-style: preserve-3d;
    }

    input[type=radio] {
      position: relative;
      top: 108%;
      left: 50%;
      width: 18px;
      height: 18px;
      margin: 0 15px 0 0;
      opacity: 0.4;
      transform: translateX(-83px);
      cursor: pointer;
    }

    input[type=radio]:nth-child(8) {
      margin-right: 0px;
    }

    input[type=radio]:checked {
      opacity: 1;
    }

    #slider label,
    #slider label img {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      color: white;
      font-size: 70px;
      font-weight: bold;
      border-radius: 3px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: transform 400ms ease;
    }

    /* Slider Functionality */
    /* Active Slide */
    #s1:checked~#slide1,
    #s2:checked~#slide2,
    #s3:checked~#slide3,
    #s4:checked~#slide4,
    #s5:checked~#slide5,
    /* add new slides*/
    #s6:checked~#slide6,
    #s7:checked~#slide7,
    #s8:checked~#slide8 {
      box-shadow: 0 13px 26px rgba(0, 0, 0, 0.3), 0 12px 6px rgba(0, 0, 0, 0.2);
      transform: translate3d(0%, 0, 0px);
    }

    /* Next Slide */
    #s1:checked~#slide2,
    #s2:checked~#slide3,
    #s3:checked~#slide4,
    #s4:checked~#slide5,
    #s5:checked~#slide6,
    /* add new slides*/
    #s6:checked~#slide7,
    #s7:checked~#slide8,
    #s8:checked~#slide1 {
      box-shadow: 0 6px 10px rgba(0, 0, 0, 0.3), 0 2px 2px rgba(0, 0, 0, 0.2);
      transform: translate3d(20%, 0, -100px);
    }

    /* Next to Next Slide */
    #s1:checked~#slide3,
    #s2:checked~#slide4,
    #s3:checked~#slide5,
    #s4:checked~#slide6,
    #s5:checked~#slide7,
    /* add new slides*/
    #s6:checked~#slide8,
    #s7:checked~#slide1,
    #s8:checked~#slide2 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(40%, 0, -250px);
    }

    /* Previous to Previous Slide */
    #s1:checked~#slide4,
    #s2:checked~#slide5,
    #s3:checked~#slide6,
    #s4:checked~#slide7,
    #s5:checked~#slide8,
    /* add new slides*/
    #s6:checked~#slide1,
    #s7:checked~#slide2,
    #s8:checked~#slide3 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(-40%, 0, -250px);
    }
    
    /* Previous to Previous Slide */
    #s1:checked~#slide5,
    #s2:checked~#slide6,
    #s3:checked~#slide7,
    #s4:checked~#slide8,
    #s5:checked~#slide1,
    /* add new slides*/
    #s6:checked~#slide2,
    #s7:checked~#slide3,
    #s8:checked~#slide4 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(-40%, 0, -250px);
    }
    
    #s1:checked~#slide6,
    #s2:checked~#slide7,
    #s3:checked~#slide8,
    #s4:checked~#slide1,
    #s5:checked~#slide2,
    /* add new slides*/
    #s6:checked~#slide3,
    #s7:checked~#slide4,
    #s8:checked~#slide5 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(-40%, 0, -250px);
    }
    
    #s1:checked~#slide7,
    #s2:checked~#slide8,
    #s3:checked~#slide1,
    #s4:checked~#slide2,
    #s5:checked~#slide3,
    /* add new slides*/
    #s6:checked~#slide4,
    #s7:checked~#slide5,
    #s8:checked~#slide6 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(-40%, 0, -250px);
    }
    
    #s1:checked~#slide8,
    #s2:checked~#slide1,
    #s3:checked~#slide2,
    #s4:checked~#slide3,
    #s5:checked~#slide4,
    /* add new slides*/
    #s6:checked~#slide5,
    #s7:checked~#slide6,
    #s8:checked~#slide7 {
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
      transform: translate3d(-20%, 0, -100px);
    }
  </style>
</head>

<body>
  <section id="slider">
    <input type="radio" name="slider" id="s1" checked>
    <input type="radio" name="slider" id="s2">
    <input type="radio" name="slider" id="s3">
    <input type="radio" name="slider" id="s4">
    <input type="radio" name="slider" id="s5">
    <input type="radio" name="slider" id="s6">
    <input type="radio" name="slider" id="s7">
    <input type="radio" name="slider" id="s8">

    <label for="s1" id="slide1"><img src="https://img.freepik.com/free-photo/empty-sea-beach-background_74190-313.jpg?size=626&ext=jpg" alt=""></label>
    <label for="s2" id="slide2"><img src="https://t4.ftcdn.net/jpg/03/09/56/17/360_F_309561741_65AVmSUj9PuybfZFo2McgkOw6jTxfCtc.jpg" alt=""></label>
    <label for="s3" id="slide3"><img src="https://thumbs.dreamstime.com/b/sunrise-over-beach-cancun-beautiful-mexico-40065727.jpg" alt=""></label>
    <label for="s4" id="slide4"><img src="https://media.istockphoto.com/photos/couple-relax-on-the-beach-enjoy-beautiful-sea-on-the-tropical-island-picture-id1160947136?k=20&m=1160947136&s=612x612&w=0&h=TdExAS2--H3tHQv2tc5woAl7e0zioUVB5dbIz6At0I4=" alt=""></label>
    <label for="s5" id="slide5"><img src="https://image.shutterstock.com/image-photo/chairs-umbrella-palm-beach-tropical-260nw-559599520.jpg" alt=""></label>
    <label for="s6" id="slide6"><img src="https://media.istockphoto.com/photos/tropical-beach-in-punta-cana-dominican-republic-caribbean-island-picture-id1150862958?b=1&k=20&m=1150862958&s=170667a&w=0&h=Fp6mNIh4pWJBBnm4dkpQfi-rjU-9D-HzEAV5Zgyc6wE=" alt=""></label>
    <label for="s7" id="slide7"><img src="https://cdn.pixabay.com/photo/2016/10/18/21/22/beach-1751455__480.jpg" alt=""></label>
    <label for="s8" id="slide8"><img src="https://img.freepik.com/free-photo/beautiful-tropical-empty-beach-sea-ocean-with-white-cloud-blue-sky-background_74190-13665.jpg?size=626&ext=jpg" alt=""></label>
  </section>


    </html>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related