Javascript 使用jquery-Loop仅返回数组中的最后一个值创建旋转木马效果

Javascript 使用jquery-Loop仅返回数组中的最后一个值创建旋转木马效果,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我试图创建旋转木马效果,但我的循环只返回数组中的最后一个值。我想在一个特定的时间后重复每一项。下面是一个带有代码的示例 $(函数(){ //无功定时器=3000; 变量$items=$('img','.container'); var currentIdx=0; var cycleItems=函数(){ log('Running from cycleItems'); var项目=[]; 对于(设i=0;i

我试图创建旋转木马效果,但我的循环只返回数组中的最后一个值。我想在一个特定的时间后重复每一项。下面是一个带有代码的示例

$(函数(){
//无功定时器=3000;
变量$items=$('img','.container');
var currentIdx=0;
var cycleItems=函数(){
log('Running from cycleItems');
var项目=[];
对于(设i=0;i<$items.length;i++){
var item=$items[i];
setTimeout(函数(){
log('我们在这里:${item}');
log('我们在这个索引上:'+currentIdx);
if($(item.hasClass('isHidden')){
$(item).removeClass('isHidden').addClass('isActive'))
}
currentIdx++
}, 3000);
}
}
/*$item.removeClass('isHidden').addClass('isActive').setTimeout(函数(){$(this).animate({scrollLeft:200+'px'}),'500','swing',函数(){console.log('Animation completed')})*/
cycleItems();
});
html{
框大小:边框框;
}
*,
*:之前,
*:之后{
框大小:继承;
}
身体{
背景:黑色;
}
.集装箱{
显示:内联;
//边框:1px纯白;
}
.slide{}.isActive{
能见度:可见;
}
伊希登先生{
可见性:隐藏;
}

试试这个:

$(function() {
//var timer = 3000;

var $items = $('img','.container');
var currentIdx = 0;

var cycleItems = function() {
console.log('Running from cycleItems');

  $items.each(function(index, item){
    setTimeout(function() {

      console.log(`We are at this item: ${index}`);
      console.log('And we are at this index: ' + currentIdx );
      $(item).removeClass('isHidden').addClass('isActive') 
      /*if ( $(item).hasClass('isHidden') ) {

       } */
      currentIdx++
     }, 3000*index);
 });
}

     /*$item.removeClass('isHidden').addClass('isActive).setTimeout(function()   { $( this ).animate( { scrollLeft: 200 + 'px' } ), '500', 'swing',     function() { console.log('Animation completed') } }) */
cycleItems();
});

将计时器与索引相乘很重要,否则计时器只能工作一次。

很高兴我能提供帮助!干杯;-)请不要编辑标题:@Mike McCaughan Ooops抱歉。谢谢