Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiperJS: Custom speed/delay for each slide?

Tags:

swiper.js

  • Swiper Version: 5.3.

I would like to know whether there is an option to set individual slide duration/delay in Swiper JS out of the box. I searched in the docs, github issues but couldn't find anything related.

If not available out of the box, can anyone show how to achieve this by hacking the core or custom code.

thanks.

like image 891
appu Avatar asked Oct 16 '25 12:10

appu


1 Answers

data-swiper-autoplay

data-swiper-autoplay attribute:

https://swiperjs.com/swiper-api#param-autoplay

Delay between transitions (in ms). If this parameter is not specified, auto play will be disabled

If you need to specify different delay for specific slides you can do it by using data-swiper-autoplay (in ms) attribute on slide:

<!-- hold this slide for 2 seconds -->
<div class="swiper-slide" data-swiper-autoplay="2000">

Example:

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: flex;
      justify-content: center;
      align-items: center;
    }
<!-- Link Swiper's CSS -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swiper-bundle.min.css">
</head>

<body>
  <!-- Swiper -->
  <h1>Swiper 7 data-swiper-autoplay</h1>
  <div class="swiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" data-swiper-autoplay="3000">3 seconds</div>
      <div class="swiper-slide" data-swiper-autoplay="4000">4 seconds</div>
      <div class="swiper-slide" data-swiper-autoplay="5000">5 seconds</div>
    </div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </div>

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

  <!-- Initialize Swiper -->
  <script>
    var swiper = new Swiper('.swiper', {
      initialSlide: 1,
      slidesPerView: 1,
      spaceBetween: 20,
      autoplay: {
        delay: 2000,
      },
      centeredSlides: true,
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
    });
  </script>
like image 56
Ezra Siton Avatar answered Oct 19 '25 11:10

Ezra Siton