LoginSignup
7
7

More than 5 years have passed since last update.

slick.js マウススクロールでスライドを送れるようにする

Posted at

概要

カルーセルを簡単に作れるライブラリ、slick.jsをマウスホイール/トラックパッドのスクロールに対応させたのでメモ。

実装

app.js
var $carousel = $('.js-carousel');

// slickの初期化
$carousel.slick({
        arrows: false,
        centerMode: true,
        centerPadding: '40px',
        slidesToShow: 3
      });

// スクロールイベントの監視
$carousel.on('wheel', function(e) {
  e.preventDefault();

  if (!$carousel.hasClass('js-slick-moving')) {
    if (e.originalEvent.deltaY < 0) {
      $(this).slick('slickNext');
    } else {
      $(this).slick('slickPrev');
    }
  }
})

// スライド送り中を判定するためにクラスを付与する
$carousel.on('beforeChange', function(){
  $carousel.addClass('js-slick-moving');
});

$carousel.on('afterChange', function(){
  $carousel.removeClass('js-slick-moving');
});
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7