Disable Prev Control on first slide and disable Next control on last slide
Asked Answered
A

10

16

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.

Aculeate answered 18/11, 2015 at 20:19 Comment(0)
S
14

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;
}
Sagacious answered 18/11, 2015 at 20:47 Comment(8)
this is so very close, when I implement this solution however my slider breaks out of its container. There are 3 slides each slide has 3 rows of 5. Here is a fiddle to illustrate jsfiddle.net/gward90/b25jgyox/4Aculeate
I added an edit on how to implement the after change function, I couldn't edit the fiddle for some reasons.Sagacious
The same issue of the slider breaking is happening @Raphael ParentAculeate
Can you make the fiddle work like before. I'll try and make it work from that.Sagacious
thanks jsfiddle.net/gward90/b25jgyox/8 Make sure the fiddle run window is greater than 768px the arrows will only show up there.Aculeate
Ok I edited my answer with the right code. I have problem with JSFiddle so I can't update your fiddle I'm sorry for that.Sagacious
Thank you very much, this gives me plenty to work from I will customize as necessary.Aculeate
Next time, give a little more research in the documentation of what you are using. There was no onAfterChange property but it was an event. It could have saved us both time but I'm happy I could help :)Sagacious
I
36

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;
}
Insolvable answered 16/8, 2016 at 23:22 Comment(3)
is it true that in ie10 pointer-events:none; doesn't work? So in this old browser how could I disable effectively the arrow?Peevish
This was a very easy solution to implement. Thanks!Fermin
This is the best and easiest solution.Albright
S
14

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;
}
Sagacious answered 18/11, 2015 at 20:47 Comment(8)
this is so very close, when I implement this solution however my slider breaks out of its container. There are 3 slides each slide has 3 rows of 5. Here is a fiddle to illustrate jsfiddle.net/gward90/b25jgyox/4Aculeate
I added an edit on how to implement the after change function, I couldn't edit the fiddle for some reasons.Sagacious
The same issue of the slider breaking is happening @Raphael ParentAculeate
Can you make the fiddle work like before. I'll try and make it work from that.Sagacious
thanks jsfiddle.net/gward90/b25jgyox/8 Make sure the fiddle run window is greater than 768px the arrows will only show up there.Aculeate
Ok I edited my answer with the right code. I have problem with JSFiddle so I can't update your fiddle I'm sorry for that.Sagacious
Thank you very much, this gives me plenty to work from I will customize as necessary.Aculeate
Next time, give a little more research in the documentation of what you are using. There was no onAfterChange property but it was an event. It could have saved us both time but I'm happy I could help :)Sagacious
J
4

A very simple solution is:

  1. set the infinite to false. eg

    $('.my-slider').slick({
     infinite: false });
    
  2. add the following css

    .slick-disabled { display: none; pointer-events:none; }

and that is it.

Jonejonell answered 16/11, 2017 at 1:14 Comment(0)
S
3

Very Simple Solution in Slick Slider Settings

$('.my-slider').slick({
     infinite: false 
});

Above settings will disable PREV and NEXT Arrows. But for hiding disabled arrows you need below CSS:

.slick-disabled {
    display: none !important;
}
Showpiece answered 9/10, 2020 at 8:25 Comment(2)
love that solutionGoshawk
visibility: hidden gets your there as well and keeps the layout fixed.Ultrasound
D
2

There is a simple Way to style the Prev-/Next-Buttons if the first or last Item is reached. Make shure that the Infinite Mode is set to false. Then style the Buttons like this:

.slick-arrow[aria-disabled="true"]{
	opacity: .5;
}

That's all you need!

Dinothere answered 24/4, 2017 at 9:57 Comment(0)
U
2

Add CSS code:

.slick-disabled {
    display: none !important;
}
Unpin answered 14/11, 2018 at 14:52 Comment(0)
N
1

I modified the behavior of slick.js directly, like so:

// making the prevArrow not show on init:
if (_.options.infinite !== true) {
    _.$prevArrow
    .addClass('slick-disabled')
    .attr('aria-disabled', 'true')
    .css('display', 'none'); // The new line of code!
}

And:

if (_.options.arrows === true &&
    _.slideCount > _.options.slidesToShow &&
    !_.options.infinite
) {
    _.$prevArrow.removeClass('slick-disabled').attr('aria-disabled', 'false').css('display', '');
    _.$nextArrow.removeClass('slick-disabled').attr('aria-disabled', 'false').css('display', '');

    if (_.currentSlide === 0) {
        _.$prevArrow.addClass('slick-disabled').attr('aria-disabled', 'true').css('display', 'none');
        _.$nextArrow.removeClass('slick-disabled').attr('aria-disabled', 'false').css('display', '');
    } else if (_.currentSlide >= _.slideCount - _.options.slidesToShow && _.options.centerMode === false) {
        _.$nextArrow.addClass('slick-disabled').attr('aria-disabled', 'true').css('display', 'none');
        _.$prevArrow.removeClass('slick-disabled').attr('aria-disabled', 'false').css('display', '');
    } else if (_.currentSlide >= _.slideCount - 1 && _.options.centerMode === true) {
        _.$nextArrow.addClass('slick-disabled').attr('aria-disabled', 'true').css('display', 'none');
        _.$prevArrow.removeClass('slick-disabled').attr('aria-disabled', 'false').css('display', '');
    }
}

Just a matter of using the jquery .css() method and setting display. Seems to work quite dandily.

Niece answered 4/11, 2016 at 17:30 Comment(0)
T
0

another way to do it is to slide to the same slide when you swipe(previous) on the first slide or swipe(next) on the last slide. Use swiper method swiper.SlideTo(n,n,m);

Trump answered 6/4, 2017 at 5:10 Comment(1)
Yes, that is the other way to do it, but it's not what the OP wants to do.Delight
Q
0

Folks,

Put

 arrows: false,

in setting JSON config. So simple!!

Quass answered 15/2, 2020 at 10:20 Comment(0)
A
0

For react and custom arrow.

const NxtArrow = ({ className, style, onClick }: any) => (
<div className={'slick-arrows2 slick-next2'} onClick={onClick}>
  <img
    style={{
      filter: onClick === null ? 'grayscale(100%)' : 'none',
      cursor: onClick === null ? 'not-allowed' : 'pointer',
    }}
    className='sliderArrowsCustom'
    src={rightArrow}
    alt='leftarrow'
  />
</div> );

  const PrvArrow = ({ className, style, onClick }: any) => (
<div className={'slick-arrows2 slick-prev2'} onClick={onClick}>
  <img
    style={{
      filter: onClick === null ? 'grayscale(100%)' : 'none',
      cursor: onClick === null ? 'not-allowed' : 'pointer',
    }}
    className='sliderArrowsCustom'
    src={leftArrow}
    alt='leftarrow'
  />
</div>  );

const settings = {
infinite: false,
nextArrow: <NxtArrow />,
prevArrow: <PrvArrow />,
}
Assert answered 31/7, 2023 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.