'Swiper.js will not work on top of css animation

I am trying to make a slider using Swiper.js.

Ive managed to get the slider to work but i would like to use CSS @keyframes animation in order to make the slider automatically slide in one direction infinitely.

The problem is that when i apply the CSS the slider breaks and no longer has any of the functionality provided by swiper.js

Here is my code

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Swiper demo</title>
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"
    />
    <!-- Link Swiper's CSS -->
    <link
      rel="stylesheet"
      href="https://unpkg.com/swiper/swiper-bundle.min.css"
    > 

    <!-- Demo styles -->
    <style>
      body {
        min-height: 100vh;
        display: grid;
        place-items: center;
      }

      .swiper {
        height: 250px;
        margin: auto;
        position: relative;
        width: 90%;
        display: grid;
        place-items: center;
        overflow: hidden;
      }

    
      .slide-track {
        display: flex;
        width: calc(250px * 9);
        animation: scroll 40s linear infinite; 
      }

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


      .swiper-slide {
        height: 200px;
        width: 250px;
        display: flex;
        align-items: center;
        padding: 15px;
        perspective: 100px;
      }

      .swiper-slide img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

     
    </style>
  </head>

  <body>
    <!-- Swiper -->
    <div class="swiper">
      <div class="slide-track">
        <div class="swiper-slide">
          <img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg">
        </div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        <div class="swiper-slide"><img src ="https://cdn.pixabay.com/photo/2022/03/20/16/21/iguana-7081261_960_720.jpg"></div>
        
    <!-- Swiper JS -->
     <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script> 

    <!-- Initialize Swiper -->
    <script>
        var swiper = new Swiper(".mySwiper", {
        slidesPerView: 4,
        spaceBetween: 30,
        loop: true,
        freeMode: true
      }); 
    </script>
  </body>
</html>



Solution 1:[1]

You might be able to do that using the swiper.js builtin functions instead.

loop: true,
autoplay: {
  delay: 1,
  disableOnInteraction: false
},
speed: 4000,

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Swiper demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />
  <!-- Link Swiper's CSS -->
  <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" />

  <!-- Demo styles -->
  <style>
    html,
    body {
      position: relative;
      height: 100%;
    }
    
    body {
      background: #eee;
      font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
      font-size: 14px;
      color: #000;
      margin: 0;
      padding: 0;
    }
    
    .swiper {
      width: 100%;
      height: 100%;
    }
    
    .swiper-slide {
      text-align: center;
      font-size: 18px;
      background: #fff;
      /* Center slide text vertically */
      display: -webkit-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
      -webkit-box-pack: center;
      -ms-flex-pack: center;
      -webkit-justify-content: center;
      justify-content: center;
      -webkit-box-align: center;
      -ms-flex-align: center;
      -webkit-align-items: center;
      align-items: center;
    }
    
    .swiper-slide img {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    
    .swiper-wrapper {
      transition-timing-function: linear;
    }
  </style>
</head>

<body>
  <!-- Swiper -->
  <div class="swiper mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
      <div class="swiper-slide">Slide 4</div>
      <div class="swiper-slide">Slide 5</div>
      <div class="swiper-slide">Slide 6</div>
      <div class="swiper-slide">Slide 7</div>
      <div class="swiper-slide">Slide 8</div>
      <div class="swiper-slide">Slide 9</div>
    </div>
    <div class="swiper-pagination"></div>
  </div>

  <!-- Swiper JS -->
  <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>


  <!-- Initialize Swiper -->
  <script>
    var swiper = new Swiper(".mySwiper", {
      slidesPerView: 3,
      spaceBetween: 30,
      loop: true,
      autoplay: {
        delay: 1,
        disableOnInteraction: false
      },
      slidesPerView: "auto",
      speed: 4000,
      pagination: {
        el: ".swiper-pagination",
        clickable: true
      }
    });
  </script>
</body>

</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lars