Home > database >  How to restart a CSS animation automatically
How to restart a CSS animation automatically

Time:10-13

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 :)

CodePudding user response:

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>

CodePudding user response:

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

CodePudding user response:

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.

  • Related