简体   繁体   English

如何使用 setInterval 函数更改幻灯片?

[英]How to change slides using setInterval function?

This is my JS code so far.到目前为止,这是我的 JS 代码。 Right now I can click on each button on the slider and it will change to the corresponding slide.现在我可以点击滑块上的每个按钮,它会变成相应的幻灯片。

$('.slide-nav').on('click', function(e) {
e.preventDefault();
// get current slide
var current = $('.flex--active').data('slide'),
  // get button data-slide
  next = $(this).data('slide');
console.log(current);
$('.slide-nav').removeClass('active');
$(this).addClass('active');

if (current === next) {
  return false;
} else {
  $('.slider__wrapper').find('.flex__container[data-slide=' + next + ']').addClass('flex--preStart');
  $('.flex--active').addClass('animate--end');
  setTimeout(function() {
    $('.flex--preStart').removeClass('animate--start flex--preStart').addClass('flex--active');
    $('.animate--end').addClass('animate--start').removeClass('animate--end flex--active');
   }, 900);
  }
 });

the slider I am working with looks like this one to see the html/css https://codepen.io/mikun/pen/YWgqEX我正在使用的滑块看起来像这样,可以看到 html/css https://codepen.io/mikun/pen/YWgqEX

So in addition to clicking to change slides I would like to scroll to change slides因此,除了单击以更改幻灯片之外,我还想滚动以更改幻灯片

You can simulate the click function this way:您可以通过以下方式模拟点击功能:

setInterval(function () {
  $(".slide-nav.active").next().click();
}, 1000);

You need to check if this is the last and try this:您需要检查这是否是最后一次并尝试以下操作:

setInterval(function () {
  if ($(".slide-nav.active").next().length > 0)
    $(".slide-nav.active").next().click();
  else
    $(".slide-nav").first().click();
}, 1000);

Demo: https://codepen.io/anon/pen/ZwBVzo演示: https : //codepen.io/anon/pen/ZwBVzo

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

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