Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slick.js on last slide show alert

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.

like image 862
friendly_llama Avatar asked Jun 14 '15 11:06

friendly_llama


People also ask

How to create slick slider using JavaScript?

For creating a slick slider just declare many elements with the same class name. index of the displayed image using JavaScript. the slideIndex will increase or decrease according to it. And the image will be displayed. Display = “none” will hide the pictures and display = “block” will again show the pictures.

How do I call slick methods on slick instances?

Methods are called on slick instances through the slick method itself in version 1.4, see below: Add a slide. If an index is provided, will add at that index, or before if addBefore is set. If no index is provided, add to the end or to the beginning if addBefore is set. Accepts HTML String || Object Remove slide by index.

How to disable slick at a given breakpoint?

Set settings to "unslick" instead of an object to disable slick at a given breakpoint. Setting this to more than 1 initializes grid mode. Use slidesPerRow to set how many slides should be in each row. In slick 1.4, callback methods have been deprecated and replaced with events.

When does slick fire?

When slider is destroyed, or unslicked. Fires when an edge is overscrolled in non-infinite mode. Fires after first initialization. Methods are called on slick instances through the slick method itself in version 1.4, see below: Add a slide. If an index is provided, will add at that index, or before if addBefore is set.


1 Answers

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

like image 65
Anima-t3d Avatar answered Sep 20 '22 00:09

Anima-t3d