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.
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.
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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With