Solution
You can add the css pointer-events: none
to your button. That css property disable all event on an element. So something like that.
// Slick stuff
...
onAfterChange: function(slide, index) {
if(index === 4) {
$('.slick-next').css('pointer-events', 'none')
}
else {
$('.slick-next').css('pointer-events', 'all')
}
}
And something on the same guideline for .slick-prev
element.
In that solution you should get the function out of the config and only put a reference to it with something like this.
// Inside slick config
onAfterChange: afterChange
// Below somewhere
var afterChange = function(slide, index) { //stuff here }
Also check to see if there is a way to get the length of the slider and verify it instead of hardcoding 4
in the if
statement. You can probably do something like $('.slide').length
Edit
Here's how to implement the afterChange()
function.
$(document).ready(function(){
$('.scrollable').slick({
dots: false,
slidesToShow: 1,
slidesToScroll: 3,
swipeToSlide: true,
swipe: true,
arrows: true,
infinite: false,
});
$('.scrollable').on('afterChange', function (event, slick, currentSlide) {
if(currentSlide === 2) {
$('.slick-next').addClass('hidden');
}
else {
$('.slick-next').removeClass('hidden');
}
if(currentSlide === 0) {
$('.slick-prev').addClass('hidden');
}
else {
$('.slick-prev').removeClass('hidden');
}
})
});
And some CSS, If you wanna do animation you should do it here.
.slick-prev.hidden,
.slick-next.hidden {
opacity: 0;
pointer-events:none;
}