skip to Main Content

I have the code for an automatic pure HTML and CSS slider, but im struggling to make it go in the opposite direction, it originally goes from right to left but im trying to make it go from left to right.

This is the original code https://codepen.io/kevinwitkowski/pen/MWbxNGe

This is my "version" https://codepen.io/sappho7124/pen/Rwewqpd

I changed it so its pure CSS instead of SCSS and changed from px to vw so its more responsive, other than that its the same code, you can see in my codepen that i have tried changing a couple things in the bottom row try to make it work to no avail.

I tried changing flex-direction: row-reverse; justify-content: flex-end; and the direction of the animation but nothing seems to work.

EDIT: It does work in the sense that the animation plays but its not showing the items as i want it, there is some kind of gap that is not there when the animation plays from right to left

This is the code from the original so you dont need to go to the codepen

HTML

<div class="slider">
    <div class="slide-track">
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/2.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/3.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/4.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/5.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/6.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/7.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/1.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/2.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/3.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/4.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/5.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/6.png" height="100" width="250" alt="" />
        </div>
        <div class="slide">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/7.png" height="100" width="250" alt="" />
        </div>
    </div>
</div>

SCSS

body {
    align-items: center;
    justify-content: center;
}

$animationSpeed: 40s;

@keyframes scroll {
    0% { transform: translateX(0); }
    100% { transform: translateX(calc(-250px * 7))}
}

.slider {
    height: 100px;
    margin: auto;
    overflow:hidden;
    position: relative;
    width: auto;
    
    .slide-track {
        animation: scroll $animationSpeed linear infinite;
        display: flex;
        width: calc(250px * 14);
    }
    .slide {
        height: 100px;
        width: 250px;
    }
}

2

Answers


  1. if you want it from going in reverse from left to right you can add reverse in the animation.

    animation: scroll $animationSpeed reverse linear infinite;

    Login or Signup to reply.
  2. You can do scroll effect with also animation:scroll and @keyframes attributes in CSS. I edited a code like below. I hope it works for you.

    * {
        box-sizing: border-box;
      }
      
      body {
        margin: 0;
      }
      
     
      .marquee-content {
        display: flex;
        animation: scroll 10s linear reverse infinite;
      }
      
      .marquee-item {
        flex: 0 0 16vw;
        margin: 0 1vw;
      }
      
      .marquee-item img {
        display: block;
        width: 100%;
     
      }
      
      @keyframes scroll {
        0% { transform: translateX(0); }
        100% { transform: translatex(-144vw); }
      }
      
        0% { transform: translateX(0); }
        100% { transform: translatex(-144vw); }
      }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
      <div class="marquee">
        <div class="marquee-content"> 
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/2.png" height="100" width="250" alt="" />
          </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/3.png" height="100" width="250" alt="" />
          </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/4.png" height="100" width="250" alt="" />
          </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/5.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/6.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/7.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/1.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/2.png" height="100" width="250" alt="" />
        </div>   
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/3.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/4.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/5.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/6.png" height="100" width="250" alt="" />
        </div>
          
          <div class="marquee-item">
            <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/557257/7.png" height="100" width="250" alt="" />
        </div>
          
        </div>
      </div>
    </body>
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search