简体   繁体   English

Slick Slider - 如果显示所有幻灯片,则 slick-current class 在导航时不会移动

[英]Slick Slider - slick-current class is not moving when navigating if all slides are shown

I have a simple slider with Slick.js ( https://kenwheeler.github.io/slick/ )我有一个简单的 slider 和 Slick.js ( https://kenwheeler.github.io/slick/ )

The slider arrows are hidden by default when all slide-items are shown because its not necessary to slide.当显示所有幻灯片项目时,slider 箭头默认隐藏,因为它不需要滑动。 So i use custom arrows with a click function that call .slick('slickNext') or .slick('slickPrev') and its working fine.所以我使用自定义箭头点击 function 调用.slick('slickNext').slick('slickPrev')并且它工作正常。 But my issue is when all slide-items are shown and I call the functions to slide next or previous the .slick-current class on the active slide-item is not switching anymore.但我的问题是,当显示所有幻灯片项目时,我调用函数来滑动下一个或上一个活动幻灯片项目上的.slick-current class 不再切换。

My goal is that the .slick-current class still moves when I click the buttons.我的目标是当我单击按钮时.slick-current class 仍然移动。

An example: https://jsfiddle.net/0nprbj95/2/ Appreciate your help!例如: https://jsfiddle.net/0nprbj95/2/感谢您的帮助!

One solution to ensure that Slick is moving the .slick-current class is to hold the state of the current slide index and then use the slick('goTo') method to force Slick to move the class to this slide.确保 Slick 正在移动.slick-current class 的一种解决方案是保留当前幻灯片索引的 state,然后使用slick('goTo')方法强制 Slick 将 class 移动到此幻灯片。

This may not give exactly the behavior that you want because Slick will still shift the slides in its "viewport", but you might be able to tweak the behavior from there.这可能无法准确给出您想要的行为,因为 Slick 仍会在其“视口”中移动幻灯片,但您可以从那里调整行为。

$(document).ready(function(){
  var currentSlide = 0;
  var numSlides = document.getElementsByClassName('slide-item').length;

  $('.slider-items-wrapper').slick({
      slidesToShow: 4,
      slidesToScroll: 1,
      mobileFirst: true,
      infinite: true,
      arrows: false,
  });

  $('.button-prev').click(function(){
    // wrap to last slide when clicked on first slide
    currentSlide = (currentSlide - 1 + numSlides) % numSlides;
    $('.slider-items-wrapper').slick('goTo', currentSlide);
  });

  $('.button-next').click(function(){
    // wrap to first slide when clicked on last slide
    currentSlide = (currentSlide + 1) % numSlides;
    $('.slider-items-wrapper').slick('goTo', currentSlide);
  });
});

Full JSFiddle: https://jsfiddle.net/c3amxzk1/1/完整的 JSFiddle: https://jsfiddle.net/c3amxzk1/1/

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

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