admin管理员组

文章数量:1418083

I am using the slick slider for my slides now I would like to get the previous slide index, I know how to get clicked slide index as follows

$('.carousel').on('afterChange', function() {
    var dataId = $('.slick-current').attr("data-slick-index");    
    console.log(dataId);
});

so how do I get the previous slide index using Jquery?

I am using the slick slider for my slides now I would like to get the previous slide index, I know how to get clicked slide index as follows

$('.carousel').on('afterChange', function() {
    var dataId = $('.slick-current').attr("data-slick-index");    
    console.log(dataId);
});

so how do I get the previous slide index using Jquery?

Share Improve this question asked Jul 7, 2021 at 10:36 The Dead ManThe Dead Man 5,57633 gold badges125 silver badges226 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

According to the Slick Event documentation the afterChange event accepts a currentSlide argument which is an integer value which is the index of the current slide. To get the index of the previous slide, subtract 1 from this value:

$('.carousel').on('afterChange', function(e, s, currentSlideIndex) {
  let previousSlideIndex = currentSlideIndex - 1;  
  console.log(previousSlideIndex);
});

the answer provided is wrong. should just use cached variable from

.on('beforeChange', function (event, slick, currentSlide, nextSlide) {}

本文标签: javascriptHow to get previous slide index in slick slideStack Overflow