如何检测Slick Carousel onBeforeChange中的滑动方向?

Phi*_*lip 2 jquery direction carousel

我如何知道旋转木马在活动中的方向onBeforeChange.右或左?

    $('.foo').slick
        infinite: false
        onBeforeChange: (e) ->
            if right
                do_this()
            if left
                do_that()
Run Code Online (Sandbox Code Playgroud)

光滑的旋转木马

小智 6

beforeChange事件使您可以访问currentSlidenextSlide值.

它也适用于无限滑块.

所以,代码是:

$(".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";
  }
});
Run Code Online (Sandbox Code Playgroud)


Ago*_*lla 0

你必须询问该元素是否具有 dir 属性并设置为 rtl

    var attr = $('.foo').attr('dir');
    if (typeof attr !== typeof undefined && attr !== false && attr === 'rtl') {
      do_this()
    } else {
      do_that()
    }
Run Code Online (Sandbox Code Playgroud)