【Slickスライダー】スマホでサムネイルの横スクロールと自動追従させる方法


Slickを使った、連動型サムネイルの横スクロールとサムネイルの自動追従の参考サイトがあまりなかったのでメモ。

最終的な動作は以下のようになります。

![002a5b587ade530b881da2f26a836ba6.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2949803/3206573f-f13a-a3fb-79a3-6ac9f33668d3.gif)

導入や基本的なSlickのcssなどは割愛しています。不明点などあればコメントください。

http://kenwheeler.github.io/slick/

``` html
<div class="item-photo">
    <!-- 大きく表示される画像 -->
    <div class="itemimg" id="item-detail-thumbnail">
        <div class="item-detail-thumbnail-inner">
            <img src="sample.jpg" alt="">
        </div>
        <div class="item-detail-thumbnail-inner">
            <img src="sample.jpg" alt="">
        </div>
        <div class="item-detail-thumbnail-inner">
            <img src="sample.jpg" alt="">
        </div>
    </div>
    <div class="itemsubimgs" id="item-detail-thumbnail-list">
        <!-- サムネイルの一覧画像 -->
        <div class="itemsubimg">
            <img src="sample.jpg" alt="">
        </div>
        <div class="itemsubimg">
            <img src="sample.jpg" alt="">
        </div>
        <div class="itemsubimg">
            <img src="sample.jpg" alt="">
        </div>
    </div>
</div>
```

```
#item-detail-thumbnail {
  position: relative;

  .slick-counter {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 20;
    font-size: 13px;
    color: #000;
    line-height: 1.2;
  }

  .slide-item-prev-btn,
  .slide-item-next-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 20;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 40px;
    height: 60px;
    line-height: 40px;

    i {
      font-size: 20px;
      color: gray;
    }
  }

  .slide-item-prev-btn {
    left: -10px;
  }

  .slide-item-next-btn {
    right: -10px;
  }
}

#item-detail-thumbnail-list {
  .slick-list {
    overflow-x: scroll;
    white-space: nowrap;

    @media screen and (min-width: 769px) {
      overflow-x: initial;
      white-space: initial;
    }

    .slick-track {
      transform: translate3d(0px, 0px, 0px);
      display: flex;
      width: auto;
      display: inline-block;

      @media screen and (min-width: 769px) {
        width: 100%;
        display: block;
      }
    }

    .slick-current {
      border: 2px solid #b0a773;
      opacity: 0.7;
    }
  }

  .itemsubimg {
    width: 29.5%;
    border-color: transparent;
    border-width: 2px;
    float: none;

    @media screen and (min-width: 769px) {
      float: left;
    }

    &:nth-child(3n) {
      margin-right: 5%;

      @media screen and (min-width: 769px) {
        margin-right: 0;
      }
    }

    img {
      opacity: 1;
    }
  }
}
```

``` js
$(function () {
	$('#item-detail-thumbnail').on('init', function(event, slick) {
		$(this).append('<div class="slick-counter"><span class="slick-counter-current"></span> / <span class="slick-counter-total"></span></div>');
		$('.slick-counter-current').text(slick.currentSlide + 1);
		$('.slick-counter-total').text(slick.slideCount);
	})
	.slick({
		autoplay: false,
		prevArrow: '<span class="slide-item-prev-btn"><i class="fas fa-chevron-left"></i></span>',
		nextArrow: '<span class="slide-item-next-btn"><i class="fas fa-chevron-right"></i></span>',
		fade: true,
		asNavFor: "#item-detail-thumbnail-list",
	})
	.on('beforeChange', function(event, slick, currentSlide, nextSlide) {
		$('.slick-counter-current').text(nextSlide + 1);
	});

	var slidesLength = $('#item-detail-thumbnail-list .itemsubimg').length;
	$("#item-detail-thumbnail-list").slick({
			slidesToShow: slidesLength,
			asNavFor: "#item-detail-thumbnail",
			focusOnSelect: true,
	})
    .on('afterChange', function(event, slick, currentSlide){
        var ww = jQuery('body').width();
        // 768px以下に適応
        if (ww <= 768) {
            // 現在のスライドに応じて横に自動スクロール
            function moveScroll(){
                var currentPosition = $('#item-detail-thumbnail-list .itemsubimg.slick-current').offset().left;
                // console.log(currentPosition);

                $('#item-detail-thumbnail-list .slick-list').animate({
                    // 現在currentスライドのx座標位置 - 余白15px
                    scrollLeft: $('#item-detail-thumbnail-list .slick-list').scrollLeft() + currentPosition - 15
                }, 300);
            }
            moveScroll();
        };
    });
});
```