简体   繁体   English

如何实现圆滑的轮播自动播放

[英]How to implement slick-carousel autoplay

I'm pretty new to web dev and sorry if my English is broken.我对网络开发很陌生,如果我的英语坏了,我很抱歉。 I'm trying to implement Slick autoplay on this section where the tooltips should appear automatically.我正在尝试在工具提示应自动出现的这一部分实现 Slick 自动播放。 I have implemented the arrows which work perfectly, but beside this I also need the autoplay.我已经实现了完美工作的箭头,但除此之外我还需要自动播放。 Can someone give me any ideas how to slick-carousel autoplay?有人可以给我任何关于如何平滑轮播自动播放的想法吗?

[HTML] [HTML]

<section class="section showcase" id="section2">
  <div class="second-background"></div>
  <div class="map" id="pin-container">
     <div class="slider">
       <i class="fas fa-chevron-left slide-left"></i>
       <i class="fas fa-chevron-right slide-right"></i>
     </div>
  </div>
</section>

[JS] [JS]

function initServicesSlider() {
   var width = $(window).width();

   if (width <= 992) {
      $('.service-slider').slick({
         mobileFirst: true,
         touchThreshold: 10,
      });
      $('.services div .slide-right').click(function () {
         $('.service-slider').slick('slickNext');
      });
      $('.services div .slide-left').click(function () {
         $('.service-slider').slick('slickPrev');
      });
}

It has to be something with:它必须是:

$(.'service-slider').slick({
  autoplay: true,
  autoplaySpeed: 3000,
})

but I don't know how to implement it.但我不知道如何实现它。

PS: I did import slick-carousel PS:我确实导入了slick-carousel

Thanks for the help !:)谢谢您的帮助 !:)

https://i.stack.imgur.com/Wfjp3.png https://i.stack.imgur.com/Wfjp3.png

There are a few things here:这里有几件事:

  1. You need to close the if statement您需要关闭if语句
 if (width <= 992) {
      $('.service-slider').slick({
         mobileFirst: true,
         touchThreshold: 10,
      });
      $('.services div .slide-right').click(function () {
         $('.service-slider').slick('slickNext');
      });
      $('.services div .slide-left').click(function () {
         $('.service-slider').slick('slickPrev');
      });
  }
  1. $(.'service-slider') should be $('.service-slider').slick... (moving the period into the string). $(.'service-slider')应该是$('.service-slider').slick... (将句点移动到字符串中)。

  2. You are calling a class that, in your HTML above, does not exist.您正在调用一个在上面的 HTML 中不存在的类。 You just need to call $('.slider').slick... .你只需要调用$('.slider').slick...

In addition to the markup fixes #1 and #2 recommended by @nihiser, you should combine all of your Slick initialization code in one call:除了@nihiser 推荐的标记修复 #1 和 #2 之外,您应该将所有 Slick 初始化代码合并到一个调用中:

$('.service-slider').slick({
  mobileFirst: true,
  touchThreshold: 10,
  autoplay: true,
  autoplaySpeed: 3000,
});

This is assuming that you are including your slides with markup that looks like this:这是假设您包含带有标记的幻灯片,如下所示:

    <div class="slider">
       <div class="service-slider">
           <div>Slide 1 Contents</div>
           <div>Slide 2 Contents</div>
           <div>Slide 3 Contents</div>
           ...
       </div>
       <i class="fas fa-chevron-left slide-left"></i>
       <i class="fas fa-chevron-right slide-right"></i>
   </div>

You don't want to include your arrows in the <div> that contains your slides or Slick will treat them as additional slides.您不想在包含幻灯片的<div>中包含箭头,否则 Slick 会将它们视为附加幻灯片。

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

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