1

I am new at Web Designing and facing some problem in CSS. Actually, I was cloning amazon.com and creating a slider with sliding animation but the animation only runs once. I want it to run infinitely. Here's my codes:

HTML - index.html

<!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">
    <link rel="stylesheet" href="css/header.css">
    <link rel="stylesheet" href="css/slider.css">
    <title>Amazon-Great Indian Sale</title>
</head>
<body>
    <header>
        <nav>
            <div class="logo">
                <a href="/index.html"><img src="img/logo.svg" alt="logo" width="138px"></a>
            </div>
            <div class="search">
                <input type="text">
                <button>Search</button>
            </div>
            <ul>
                <div class="nav-items">
                    <li><a href="#">Mobiles</a></li>
                    <li><a href="#">Best Sellers</a></li>
                    <li><a href="#">Electronics</a></li>
                    <li><a href="#">Luggage</a></li>
                    <li><a href="#">Fashion</a></li>
                    <li><a href="#">Software</a></li>
                    <li><a href="#">Sports</a></li>
                    <li><a href="#">Hardware</a></li>
                    <li><a href="#">Furnitures</a></li>
                </div>
            </ul>
        </nav>
    </header>
    <main>
        <div class="slider" id="slide">
            <img src="img/slider_img1.jpeg">
            <img src="img/slider_img2.jpg">
            <img src="img/slider_img3.jpg">
            <img src="img/slider_img4.jpg">
        </div>
    </main>
</body>
<script src="js/slider.js"></script>
</html>

CSS - slider.css

.slider{
    display: flex;
    width: 100%;
    height: 37vh;
    overflow: hidden;
}
.slider img{
    min-width: 100%;
    min-height: 100%;
    animation: slide1 1s 2s forwards, slide2 1s 4s forwards, slide3 1s 6s forwards, slide4 1s 8s forwards;
    transform: translate(0%);
}
@keyframes slide1 {
    0%{
        transform: translate(0%);
    }
    100%{
        transform: translate(-100%);
    }
}
@keyframes slide2 {
    0%{
        transform: translate(-100%);
    }
    100%{
        transform: translate(-200%);
    }
}
@keyframes slide3 {
    0%{
        transform: translate(-200%);
    }
    100%{
        transform: translate(-300%);
    }
}
@keyframes slide4 {
    0%{
        transform: translate(-300%);
    }
    100%{
        transform: translate(0%);
    }
}

Note - I have not posted the CSS codes of header part, so you can ignore it.

I have tried doing this by JS but it didn't worked for me. Thanks in advance :)

4
  • developer.mozilla.org/en-US/docs/Web/CSS/… Commented Oct 13, 2021 at 13:24
  • @CBore I will not work in this case Commented Oct 13, 2021 at 13:27
  • Why not? And how does your slider even work in the first place, you neglected to show us your JavaScript part (which I assume will have some relevance, too, based on that script file name?) Please always provide a proper minimal reproducible example with questions like this. Commented Oct 13, 2021 at 13:30
  • @CBore I don't have any JavaScript part Commented Oct 13, 2021 at 13:46

3 Answers 3

3

Instead of multiple animations you can use a single one with multiple steps. Not just 0% and 100%. this also enables the use of infinite step count

.slider{
    display: flex;
    width: 100%;
    overflow: hidden;
}
.slider img{
    min-width: 100%;
    min-height: 100%;
    animation: slide 8s forwards infinite;
    transform: translate(0%);
}
@keyframes slide {
    0% {
      transform: translate(0%);
    }
    10% { /* wait 10% for delay */
      transform: translate(0%);
    }
    
    25% {
      transform: translate(-100%);
    }
    35% { /* wait 10% for delay */
      transform: translate(-100%);
    }
    
    50% {
      transform: translate(-200%);
    }
    60% { /* wait 10% for delay */
      transform: translate(-200%);
    }
    
    75% {
      transform: translate(-300%);
    }
    85% { /* wait 10% for delay */
      transform: translate(-300%);
    }
    
    100% {
      transform: translate(0%);
    }
}
<main>
    <div class="slider" id="slide">
        <img src="//picsum.photos/500/250?c=1" width="500" height="250">
        <img src="//picsum.photos/500/250?c=2" width="500" height="250">
        <img src="//picsum.photos/500/250?c=3" width="500" height="250">
        <img src="//picsum.photos/500/250?c=4" width="500" height="250">
    </div>
</main>

Sign up to request clarification or add additional context in comments.

2 Comments

@Reyno Thank you very much. From two days I was stuck in it, you helped me. This is the reason why I love stackoverflow. Thanks again ;)
This is an outstanding answer. Thanks so much.
0

You can use the infinite keyword to make an animation loop forever: https://developer.mozilla.org/fr/docs/Web/CSS/animation.

1 Comment

I have added multiple animation in one so it will not work in this case
0

Just simply add infinite to your animation property or animation-iteration-count: infinite; in the new line.

Example:

animation: animation-name 450ms linear infinite;
/* or */
animation-iteration-count: infinite;

Docs: https://developer.mozilla.org/en/docs/Web/CSS/animation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.