How to detect slide direction in Slick Carousel onBeforeChange?

Philip picture Philip · Dec 12, 2014 · Viewed 8.7k times · Source

How can I know what direction the carousel are going on the event onBeforeChange. right or left?

    $('.foo').slick
        infinite: false
        onBeforeChange: (e) ->
            if right
                do_this()
            if left
                do_that()

Slick Carousel

Answer

Innokenty picture Innokenty · Feb 23, 2016

beforeChange event get you an access to currentSlide and nextSlide values.

It works for infinite sliders too.

So, the code is:

$(".some-element").on('beforeChange', function (event, slick, currentSlide, nextSlide) {
  if (Math.abs(nextSlide - currentSlide) == 1) {
    direction = (nextSlide - currentSlide > 0) ? "right" : "left";
  }
  else {
    direction = (nextSlide - currentSlide > 0) ? "left" : "right";
  }
});