Slick.js: How to remove current slide?

10k views Asked by At

Using Slick.js - how i can remove current active slide? I made of an example, but nothing works

var $status = $('.pagingInfo');
var $slickElement = $('.slideshow');

$slickElement.on('init reInit afterChange', function (event, slick, currentSlide, nextSlide) {
    //currentSlide is undefined on init -- set it to 0 in this case (currentSlide is 0 based)
    var i = (currentSlide ? currentSlide : 0) + 1;
    $status.text(i + '/' + slick.slideCount);
});

$slickElement.slick({
    slide: 'img',
    speed: 1,
    infinite: false,
    swipe: false,
    autoplay: false,
    dots: false
});

$('.remove-slide').on('click', function() {
    $slickElement.slick('slickRemove', i);
});

My fiddle here - http://jsfiddle.net/q5yae1t3/9/

2

There are 2 answers

2
RoboticRenaissance On BEST ANSWER

You don't have an i variable set in the $(...).on(...) function. You need to set i in order to remove something based on i. Then you also need to change all the data-slick-index attributes, because those weren't changed when the slide counter gets changed.

$('.remove-slide').on('click', function() {
   i = $("img.slick-active").attr("data-slick-index");
   $slickElement.slick('slickRemove', i);
   var j = 0;
   $("img.slick-slide").each(function(){
     $(this).attr("data-slick-index",j);
     j++;
   });
});

http://jsfiddle.net/q5yae1t3/19/ Hope that helps!

0
manATweb On

This is a one-liner.

var currentSlide = $('.slideshow').slick('slickCurrentSlide');
$('.slideshow').slick('slickRemove', currentSlide);