简体   繁体   English

在 Slick Slider 中的“asNavFor”滑块中隐藏活动幻灯片

[英]Hide Active Slide in `asNavFor` slider in Slick Slider

Question

Is there a way to hide the currently-active slide from a slider in Slick Slider?有没有办法从 Slick Slider 中的滑块隐藏当前活动的幻灯片?

Motivation动机

I am building a Featured Posts slider.我正在构建一个精选帖子滑块。 One post will be featured prominently, while the remaining posts will feature as previews in a column on the side.一个帖子将突出显示,而其余帖子将在旁边的一栏中作为预览。 A total of 5 posts will be shown at a time.一次将显示总共 5 个帖子。

This is pretty easy to do with Slick Slider (1.9.0) but it has one issue: the "remaining posts" section should only show the remaining posts and not the current post.使用Slick Slider (1.9.0) 很容易做到这一点,但它有一个问题:“剩余帖子”部分应该显示剩余帖子而不是当前帖子。 So when a post is selected it should scroll out of view in the slider, and the featured post slider will change to use the slide at the same index.因此,当一个帖子被选中时,它应该滚动到滑块的视图之外,并且特色帖子滑块将更改为使用相同索引处的幻灯片。

Description of the Action动作描述

Basically, the initial state should be:基本上,初始状态应该是:

Featured Slider: Slide 1 is visible, all others are hidden.特色滑块:幻灯片 1 可见,所有其他滑块均隐藏。 Remaining Slider: Slide 1 is hidden, Slides 2-5 are visible.剩余滑块:幻灯片 1 隐藏,幻灯片 2-5 可见。

If I click on slide 2 from the Remaining Slider:如果我单击剩余滑块中的幻灯片 2:

Featured Slider: Slide 2 is visible, all others are hidden.特色滑块:幻灯片 2 可见,所有其他滑块均隐藏。 Remaining Slider: Slide 2 slides out of view, Slide 3 becomes the new top post followed by (in order) 4, 5, 1.剩余滑块:幻灯片 2 滑出视图,幻灯片 3 成为新的顶柱,然后是(按顺序)4、5、1。

JS Fiddle JS小提琴

Here's the JS Fiddle I'm working on: https://jsfiddle.net/zak_rhm/Lks61ah7/71/这是我正在研究的 JS Fiddle: https : //jsfiddle.net/zak_rhm/Lks61ah7/71/

HTML HTML

<div id="Large_Slider">
    <!-- Should only show one slide at a time. -->
    <article class="slide">
        <h2>Slide 1</h2>
    </article>
    <article class="slide">
        <h2>Slide 2</h2>
    </article>
    <article class="slide">
        <h2>Slide 3</h2>
    </article>
    <article class="slide">
        <h2>Slide 4</h2>
    </article>
    <article class="slide">
        <h2>Slide 5</h2>
    </article>
</div>
<ul id="Small_Slider">
    <!-- Should only show four slides at a time, never show the slide in #Large_Slider -->
    <li id="slide">Slide 1</li>
    <li id="slide">Slide 2</li>
    <li id="slide">Slide 3</li>
    <li id="slide">Slide 4</li>
    <li id="slide">Slide 5</li>
</ul>

JavaScript (jQuery) JavaScript (jQuery)

var $slider_lg = $("#Large_Slider")
var $slider_sm = $("#Small_Slider")

$slider_lg.slick({
  slidesToShow: 1,
  slidesToScroll: 1,
  asNavFor: '#Small_Slider',
  dots: true,
  fade: true
});

$slider_sm.slick({
  arrows: false,
  slidesToShow: 4,
  slidesToScroll: 1,
  asNavFor: '#Large_Slider',
  focusOnSelect: true,
  vertical: true,
  verticalSwiping: true
});

This should do what you're looking for: https://jsfiddle.net/kmsv5xn8/这应该做你正在寻找的: https : //jsfiddle.net/kmsv5xn8/

I changed your HTML so that the "Small_Slider" section begins at #2 and each slide has a data-sm-slide attribute:我更改了您的 HTML,以便“Small_Slider”部分从 #2 开始,并且每张幻灯片都有一个data-sm-slide属性:

<ul id="Small_Slider" class="slick-slider">
  <li id="sm-slide-2" class="slide slide--sm" data-sm-slide="2">
    <img src="https://via.placeholder.com/640x480.png/0f9/333?text=Slide+2" alt="Slide 2" height="48" width="64" class="slide--sm__img" />
    <span class="slide--sm__title">Slide 2</span>
  </li>
  <li id="sm-slide-3" class="slide slide--sm" data-sm-slide="3">
    <img src="https://via.placeholder.com/640x480.png/f09/fff?text=Slide+3" alt="Slide 3" height="48" width="64" class="slide--sm__img" />
    <span class="slide--sm__title">Slide 3</span>
  </li>
  <li id="sm-slide-4" class="slide slide--sm" data-sm-slide="4">
    <img src="https://via.placeholder.com/640x480.png/f90/333?text=Slide+4" alt="Slide 4" height="48" width="64" class="slide--sm__img" />
    <span class="slide--sm__title">Slide 4</span>
  </li>
  <li id="sm-slide-5" class="slide slide--sm" data-sm-slide="5">
    <img src="https://via.placeholder.com/640x480.png/90f/fff?text=Slide+5" alt="Slide 5" height="48" width="64" class="slide--sm__img" />
    <span class="slide--sm__title">Slide 5</span>
  </li>
  <li id="sm-slide-1" class="slide slide--sm" data-sm-slide="1">
    <img src="https://via.placeholder.com/640x480.png/09f/333?text=Slide+1" alt="Slide 1" height="48" width="64" class="slide--sm__img" />
    <span class="slide--sm__title">Slide 1</span>
  </li>
</ul>

That takes care of the initial offset, however to show the correct slides I disabled the focusOnSelect: true property and added an event listener instead:这会处理初始偏移量,但是为了显示正确的幻灯片,我禁用了focusOnSelect: true属性并添加了一个事件侦听器:

$slider_sm.on("click", "li.slide", function() {
  var slideNo = $(this).attr("data-sm-slide") - 1;
  $slider_lg.slick("slickGoTo", slideNo);
})

The corresponding slide index is taken from the data-* attribute and updates the main carousel;对应的幻灯片索引取自data-*属性并更新主轮播; because that uses the asNavFor property, Slick takes care of the rest.因为它使用了asNavFor属性,所以 Slick 负责其余的工作。

Just add a style in .slick-current class只需在 .slick-current 类中添加样式

#Small_Slider .slick-current {
  opacity: 1;
  visibility: hidden;
  width:0px !important;
  height: 0px !important;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM