Question

Sliding Cards in React (card component)

so, i have 5 cards in my project, how do i make them slide slowly and completely towards the left and pop out at the same pace from the right side of the screen and keep moving till they reach their default position and halt for a few seconds and then continue.

I tried some css, but it is not working satisfactorily:

.box {
    background: #beeec9a6;
    animation: slide 20s infinite;
    animation-timing-function: linear;
  }

  @keyframes _slide_ {
    0% {
      transform: translateX(100%);
    }
    15% {
      transform: translateX(0%);
    }
    85% {
      transform: translateX(0%);
    }
    100% {
      transform: translateX(-100%);
    }
  }

they are moving towards the left alright but not completely… Please anyone ?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 9, 2023

Hi there,

I could suggest trying out the following CSS:

.box {
  background: #beeec9a6;
  animation: slide 20s infinite;
  animation-timing-function: linear;
}

@keyframes _slide_ {
  0% {
    transform: translateX(100%);
  }
  20% {
    transform: translateX(0);
  }
  80% {
    transform: translateX(0);
  }
  100% {
    transform: translateX(-100%);
  }
}

Here is an example app that I’ve tested this with and it seems to be working as expected:

import React from 'react';
import './CardSlider.css'; // import the CSS file

const CardSlider = () => {
  return (
    <div className='card-container'>
      {[1, 2, 3, 4, 5].map((card, index) => (
        <div key={index} className='box'>
          Card {card}
        </div>
      ))}
    </div>
  );
};

export default CardSlider;

Here is this in action:

https://playcode.io/1500781

Hope that this helps!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
June 9, 2023

Hey @ca9aa8543ef8472cb226cc4661df64,

From your description, it seems you want to create a carousel-like effect where the cards scroll continuously from right to left, pause for a moment in their original position, then start moving again.

Looking at your keyframes, there are a couple of things that might be causing issues.

First, you’re moving your element from 100% (completely off the right side of the screen) to 0% (back in its original position) quite fast (within the first 15% of the animation duration).

Second, the element stays at 0% (in its original position) for a large chunk of the animation (from 15% to 85%) and then it quickly moves to -100% (completely off the left side of the screen).

In your case, it may be beneficial to have the element spend more time moving and less time stationary.

Here is an updated version of your keyframes animation:

@keyframes slide {
  0% {
    transform: translateX(100%);
  }
  25% {
    transform: translateX(0%);
  }
  75% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(-100%);
  }
}

In this version, the element starts moving immediately from off the right side of the screen (100%) and spends 25% of the animation duration moving to its original position (0%). It then stays in place from 25% to 75% of the animation duration. Finally, it spends the last 25% of the animation moving off the left side of the screen (-100%).

This should give a smoother movement and make it more likely for the element to move fully off the screen.

If you want the cards to spend more time in their original position, you can adjust the percentages accordingly. Just make sure that the movement phases (from 100% to 0% and from 0% to -100%) take up a sufficient portion of the animation duration to allow the cards to move fully off screen.

You might also want to check whether the parent container of your .box elements has the style overflow: hidden; to ensure that the cards disappear when they move off screen. Without this, the cards might still be partially visible, giving the impression that they haven’t moved fully off screen.

Remember to adjust the animation duration (20s in your example) to achieve the desired speed of movement. If the movement is still too fast, you can increase this value

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel