slick.js on last slide show alert
Asked Answered
C

4

8

I'm using slick.js http://kenwheeler.github.io/slick/ to run through some slides, the last of which should toggle an alert.

After some ferreting around online and realizing that this should work without having to hard code the number of slides (and having seen this solution: Slick carousel. Want autoplay to play the slides once and stop ), I've got the following code:

$(document).ready(function(){
          var item_length = $('.slider > div').length - 1;
          $('.slider').slick({
            appendArrows : '.arrow-holder',
            adaptiveHeight : true,
            infinite : false,
            onAfterChange: function(){
                if( item_length == slider.slickCurrentSlide() ){
                    alert("Slide 2");
                };
            }
          }); 
        });

Unfortunately, nothing happens when I reach the last slide. Nor does it seem to if I do hard code in the last number, even taking into account the zero indexing.

I have no errors or warnings in the console, so am struggling to work out how to figure out the problem.

Charo answered 14/6, 2015 at 11:30 Comment(0)
G
6

Your onAfterChange function is lacking the input it needs from slick. This code should fix it:

 $(document).ready(function(){
      var item_length = $('.slider > div').length - 1;
      $('.slider').slick({
        appendArrows : '.arrow-holder',
        adaptiveHeight : true,
        infinite : false,
        onAfterChange: function(slide, index){
            if( item_length == index ){
                alert("Slide 2");
            };
        }
      }); 
    });

Note that index[0] is the first slide

Gorgias answered 23/6, 2015 at 22:8 Comment(1)
This no longer works, see my answer for an updated version.Detruncate
D
11

For people from the future, the API has changed. This is what I am using now:

$(this).on('afterChange', function(event, slick, currentSlide) {
  console.log(slick, currentSlide);
  if (slick.$slides.length-1 == currentSlide) {
    console.log("Last slide");
  }
})

Note: this can be replaced with the selector e.g. .slider

Detruncate answered 8/9, 2017 at 13:30 Comment(0)
G
6

Your onAfterChange function is lacking the input it needs from slick. This code should fix it:

 $(document).ready(function(){
      var item_length = $('.slider > div').length - 1;
      $('.slider').slick({
        appendArrows : '.arrow-holder',
        adaptiveHeight : true,
        infinite : false,
        onAfterChange: function(slide, index){
            if( item_length == index ){
                alert("Slide 2");
            };
        }
      }); 
    });

Note that index[0] is the first slide

Gorgias answered 23/6, 2015 at 22:8 Comment(1)
This no longer works, see my answer for an updated version.Detruncate
G
5

This solution based on Anima-t3d's works with multiple slides at once

$(this).on('afterChange', function(event, slick, currentSlide){
    if (slick.$slides.length == currentSlide + slick.options.slidesToScroll) {
        console.log("Last slide");
    }
});
Galloway answered 11/11, 2017 at 23:39 Comment(1)
You can use options.slidesToShow If options.slidesToScroll is 1.Catina
F
2

Live demo

This is what you need!

$('#myslick').on('afterChange', function(event, slick, currentSlide) {
      if (slick.currentSlide >= slick.slideCount - slick.options.slidesToShow) {
        alert("Last slide!!!");
      }
});
Frizz answered 5/6, 2021 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.