Slick carousel:滑动可变数量的元素

Slick carousel: Slide variable number of elements

我想使用灵活的滑块在可变数量的元素之间滑动。 原因是,我不知道有多少张幻灯片适合屏幕宽度。所以我不想设置固定数量的滑块项。

这是我对普通列表和滚动溢出所做的操作。 但我需要一个右边的箭头按钮。因此我会使用光滑的旋转木马。或者有什么更好的解决办法吗?

这是我当前的代码(工作示例): https://codepen.io/cray_code/pen/WNvXRXP

.slider-subcats-wrapper {
    width: 100%;
    height: 100px;
  padding: 20px 0;
    overflow-x: scroll;
    overflow-y: hidden;
    position: relative;
    white-space: nowrap;
    position: relative;
}

.slider-subcats-container {position: relative;}
.slider-subcats-container:after {
    content: "";
    position: absolute;
    height: 100px;
    width: 5rem;
    z-index: 10;
    right: -1px;
    top: 0;
    background: linear-gradient(to right,#fff0 0,#fff 78%,#fff 100%);
}

.slider-subcats {margin: 0; padding: 0; white-space: nowrap}

.slider-subcats li {
    display: inline-block;

    /* if you need ie7 support */
    *display: inline;
    zoom: 1
}


.slider-subcats-link {padding: 0.5rem 0.75rem; border: 1px solid #dee2e6; border-radius: 0.25rem; display: inline-block; font-size: 0.875rem; margin-right: 0.25rem; letter-spacing: 0.005rem;}
.slider-subcats li:last-child .slider-subcats-link {margin-right: 2.5rem;}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
 <div class="row">
  <div class="col-6 offset-3">
   <div class="slider-subcats-container">
    <div class="slider-subcats-wrapper">
     <ul class="slider-subcats">
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Item</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Longer Item</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Item with more space</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Item</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Medium</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">2</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Item again</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Item with text</a>
      </li>
      <li class="slider-subcats-item">
       <a class="slider-subcats-link" href="#">Nav</a>
      </li>
     </ul>
    </div>
   </div>
  </div>
 </div>
</div>

您不必精确设置幻灯片上的图像数量! 在 Slick Slide 的网站上查看 'Slider Syncing'。

$('.slider-nav').slick({
  slidesToShow: 3,
  slidesToScroll: 1,
  asNavFor: '.slider-for',
  dots: true,
  centerMode: true,
  focusOnSelect: true
});

slidesToShow 指示幻灯片上有多少图像,slidesToScroll 指示单击箭头时将通过多少图像。