Marcin Włodarczyk
Marcin Włodarczyk

Reputation: 33

Slick.js - multiple carousels are not synchronized on autoplay

i have created multiple carousels using Slick (https://kenwheeler.github.io/slick/) on single page. They all have the same class and setting (mainly autoplay infinite), but they change within diffrent time. I'm not able to synchronize them.

Gif with the problem: https://i.sstatic.net/NS8Hj.jpg

I have even created example: https://jsfiddle.net/ou85zmqj

$(document).ready(function() {
  $('.slick').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 2000,
    arrows: false,
    pauseOnHover: false
  });
});
.slick {
  margin: 0 auto;
  max-width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
</head>

<body>
  <script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>

  <div class="slick">
    <img src="https://picsum.photos/200/300" alt="">
    <img src="https://picsum.photos/200/301" alt="">
    <img src="https://picsum.photos/200/302" alt="">
    <img src="https://picsum.photos/200/303" alt="">
  </div>

  <div class="slick">
    <img src="https://picsum.photos/200/304" alt="">
    <img src="https://picsum.photos/200/305" alt="">
    <img src="https://picsum.photos/200/306" alt="">
    <img src="https://picsum.photos/200/307" alt="">
  </div>

  <div class="slick">
    <img src="https://picsum.photos/200/308" alt="">
    <img src="https://picsum.photos/200/309" alt="">
    <img src="https://picsum.photos/200/310" alt="">
  </div>

</body>

</html>

Can i do something to keep them synchronized?

Upvotes: 3

Views: 410

Answers (1)

Ed Lucas
Ed Lucas

Reputation: 7355

One solution is to set the first slideshow to autoplay and have it control the others by setting the asNavFor property. When the first slideshow changes, it will immediately change the other two.

The only problem with using this approach is that it requires the slideshows to have the same number of slides, otherwise it may not rotate through all the slides (if the controlling slideshow has fewer) or show the last slide more than once (if the controlling slideshow has more).

$(document).ready(function(){
  $('.slick1').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 2000,
    arrows: false,
    pauseOnHover: false,
    asNavFor: '.slick2'
  });  
  $('.slick2').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: false,
    pauseOnHover: false
  });
});
.slick {
  margin: 0 auto;
  max-width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
</head>

<body>
  <script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>

  <div class="slick slick1">
    <img src="https://picsum.photos/200/300" alt="">
    <img src="https://picsum.photos/200/301" alt="">
    <img src="https://picsum.photos/200/302" alt="">
    <img src="https://picsum.photos/200/303" alt="">
  </div>

  <div class="slick slick2">
    <img src="https://picsum.photos/200/304" alt="">
    <img src="https://picsum.photos/200/305" alt="">
    <img src="https://picsum.photos/200/306" alt="">
    <img src="https://picsum.photos/200/307" alt="">
  </div>

  <div class="slick slick2">
    <img src="https://picsum.photos/200/308" alt="">
    <img src="https://picsum.photos/200/309" alt="">
    <img src="https://picsum.photos/200/310" alt="">
    <img src="https://picsum.photos/200/311" alt="">
  </div>

</body>

</html>

Another solution is to have the first slideshow trigger the next slide of the other two slideshows, using the beforeChange event. This version will work no matter how many slides are in each slideshow.

$(document).ready(function(){
  $('.slick1').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 2000,
    arrows: false,
    pauseOnHover: false
  });  
  $('.slick2').slick({
    slidesToShow: 1,
    slidesToScroll: 1,
    arrows: false,
    pauseOnHover: false
  });
  
  // First slideshow triggers the other two to change
  $('.slick1').on('beforeChange', function(event, slick, currentSlide, nextSlide){
    $('.slick2').slick('slickNext');
  });

});
.slick {
  margin: 0 auto;
  max-width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.css" />
</head>

<body>
  <script type="text/javascript" src="//cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js"></script>

  <div class="slick slick1">
    <img src="https://picsum.photos/200/300" alt="">
    <img src="https://picsum.photos/200/301" alt="">
    <img src="https://picsum.photos/200/302" alt="">
    <img src="https://picsum.photos/200/303" alt="">
  </div>

  <div class="slick slick2">
    <img src="https://picsum.photos/200/304" alt="">
    <img src="https://picsum.photos/200/307" alt="">
  </div>

  <div class="slick slick2">
    <img src="https://picsum.photos/200/308" alt="">
    <img src="https://picsum.photos/200/309" alt="">
    <img src="https://picsum.photos/200/310" alt="">
  </div>

</body>

</html>

Upvotes: 2

Related Questions