Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Prev Control on first slide and disable Next control on last slide

using Slick and looking for a way to have the prev control disabled and hidden until the user clicks the next arrow. Similarly I want the Next control to become disabled and hidden once the user reaches the last slide.

A good illustration of what I'm trying to accomplish is here

Does slick have an attribute already for this that I have overlooked? Is this something that maybe can be accomplished using CSS by adding a disabled class?

The only thing similar I have found is this

$('.pages .slider').slick({
    autoplay: true,
    autoplaySpeed: 5000,
    infinite: false,
    onAfterChange: function(slide, index) {
        if(index == 4){
        $('.pages .slider').slickPause();

But its not exactly what I am looking for.

like image 414
gwar9 Avatar asked Nov 18 '15 20:11

gwar9


People also ask

How can I disable a button at the last slide of a carousel?

You can either set data-wrap="false" on the carousel or just put it in the options when you initialize the carousel.

How do you hide the old and next button on slick slider?

slick({ dots: false, prevArrow: false, nextArrow: false });

How do you get the current slide on slick slider?

slick({ lazyLoad: 'progressive', onAfterChange: function(slider,index){ console. log(???); } }); Where slider returns the carousel object and index returns the current slide.


2 Answers

Just use infite: false and create a custom css for class slick-disabled. Eg.

JS - jQuery

$('.my-slider').slick({
    infinite: false,
    slidesToShow: 1,
    slidesToScroll: 1
});

CSS

.slick-disabled {
    opacity: 0;
    pointer-events:none;
}
like image 79
Jeff Monteiro Avatar answered Sep 24 '22 04:09

Jeff Monteiro


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;
}
like image 42
Raphael Parent Avatar answered Sep 24 '22 04:09

Raphael Parent