简体   繁体   English

如何使流畅的 slider 连续自动播放,但加快箭头/点单击时的滚动速度?

[英]How to make a slick slider continuously autoplay, but speed up scroll on arrow/dot click?

I would like to make a slider, which is continuously autoplay.我想制作一个连续自动播放的 slider。 It works fine, but the arrows and dots don't look to work properly.它工作正常,但箭头和点看起来不能正常工作。 I'm not sure which of the settings should I change, but I would like the slider to instantly scroll after clicking on the arrow or dot.我不确定应该更改哪些设置,但我希望 slider 在单击箭头或点后立即滚动。

I was trying to change some settings with the code below, but nothing seems to work fine.我试图用下面的代码更改一些设置,但似乎没有任何效果。

When speed is set to 6000 autoplay has perfect timing, but the same slow speed is used on scrolling after clicking on the dot/arrow and this behavior is unwanted (it should scroll immediately to chosen slide).speed设置为6000时,自动播放具有完美的时机,但在单击点/箭头后滚动时使用相同的慢速,并且这种行为是不需要的(它应该立即滚动到选定的幻灯片)。 Could you please help me to understand what should I do to achieve my goal?你能帮我理解我应该怎么做才能实现我的目标吗?

 $(document).ready(function(){ $('.test').slick({ slidesToShow: 3, slidesToScroll: 1, autoplay: true, autoplaySpeed: 1, speed: 6000, dots: true, cssEase: 'linear', waitForAnimate: false, pauseOnFocus: false, pauseOnHover: false }); });
 .slick-prev:before, .slick-next:before { color: black;important; }
 <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/> <link rel="stylesheet" href="https://cdn.jsdelivr.net/jquery.slick/1.5.0/slick-theme.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script> <div style='margin-left:20px;width:200px;color:black;important;'> <div class='test'> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </div> </div>

Some JS options may not work with another setting (as it does in SwiperJS) so as you go through each added JS function watch for it to potentially negate another earlier setting.某些 JS 选项可能无法与其他设置一起使用(就像在 SwiperJS 中一样),因此当您 go 通过每个添加的 JS function 观察它可能会否定另一个早期设置。 The following should get you on track (as an example).以下应该让你走上正轨(作为例子)。

<section class="slider">
<div>
  <img src="https://via.placeholder.com/200x200.gif/?text=1">
</div>

<script type="text/javascript">
$('.slider').slick({
  dots: true,
  infinite: true,
  slidesToShow: 1,
  slidesToScroll: 1,
  autoplay: true,
  autoplaySpeed: 800,
  pauseOnFocus: false, 
  pauseOnHover: true
});
</script>

You can achieve this behavior changing the autoplaySpeed to 6000 and the speed to 500, your code will look like this:您可以实现此行为,将autoplaySpeed更改为 6000 并将speed更改为 500,您的代码将如下所示:

$(document).ready(function(){
  $('.test').slick({
    slidesToShow: 3,
    slidesToScroll: 1,
    autoplay: true,
    autoplaySpeed: 6000,
    speed: 500,
    dots: true,
    cssEase: 'linear',
    waitForAnimate: false,
    pauseOnFocus: false, 
    pauseOnHover: false
  });
});

You can modify the transition speed on arrow/dot click by updating the speed property in the slick function. Here is an example in jQuery:您可以通过更新光滑 function 中的速度属性来修改箭头/点单击的转换速度。这是 jQuery 中的示例:

 $('.your-slider').slick({ autoplay: true, autoplaySpeed: 6000, speed: 1000, dots: true, arrows: true, }); $('.slick-arrow').on('click', function() { $('.your-slider').slick('slickSetOption', 'speed', 1000, true); }); $('.slick-dots button').on('click', function() { $('.your-slider').slick('slickSetOption', 'speed', 1000, true); });

This code sets the autoplay speed to 6000ms, but when the arrow or dot is clicked, it changes the transition speed to 1000ms.此代码将自动播放速度设置为 6000 毫秒,但是当单击箭头或点时,它会将转换速度更改为 1000 毫秒。 You can adjust the speed value to your desired transition speed on arrow/dot click.您可以在箭头/点单击上将速度值调整为所需的过渡速度。

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

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