使用jquery创建轮播效果-循环仅返回数组中的最后一个值

互编码器

我正在尝试创建轮播效果,但是我的循环仅返回数组内的最后一个值。我想在特定时间后遍历每个项目。这是一个带有代码codepen

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

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

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

    for (let i = 0; i < $items.length; i++) {
      var item = $items[i];

      setTimeout(function() {
        console.log('We are at this item: ${item}');
        console.log('And we are at this index: ' + currentIdx);

        if ($(item).hasClass('isHidden')) {
          $(item).removeClass('isHidden').addClass('isActive')
        }
        currentIdx++
      }, 3000);
    }
  }


  /*$item.removeClass('isHidden').addClass('isActive).setTimeout(function() { $( this ).animate( { scrollLeft: 200 + 'px' } ), '500', 'swing', function() { console.log('Animation completed') } }) */
  cycleItems();
});
html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
body {
  background: black;
}
.container {
  display: inline;
  //border: 1px solid white;

}
.slide {} .isActive {
  visibility: visible;
}
.isHidden {
  visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class=container>
  <img class='isActive' src="http://placehold.it/350x150">
  <img class='isHidden' src="http://placehold.it/350x150">
  <img class='isHidden' src="http://placehold.it/350x150">
  <img class='isHidden' src="http://placehold.it/350x150">
</div>

玛鲁恩穿越

试试这个:

$(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();
});

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AngularJS和JQuery $ .each()函数仅返回最后一个循环值

在函数中使用for循环,仅返回最后一个值

jQuery .json中的JSON数组,当使用.append时,仅显示数组的最后一个值

Ruby for循环仅使用最后一个值

仅循环使用最后一个值

PHP循环-使用数组中的最后一个非空白值

遍历数组以对样式元素使用数据属性,但仅获取数组中的最后一个值

R:在矩阵中创建从 1 到 10 的对角线 -> 仅返回最后一个值

我创建了一个jquery轮播,并使用if条件运行和停止轮播

一个 SimpleStackDemo,并使用循环从数组中返回值,LIFO

Flask 使用 boto3 仅返回最后一个值或第一个值

jQuery 工具提示在循环中设置动态内容返回最后一个值

当我尝试使用循环在 Excel 工作表中写入行和列时,仅打印最后一个循环值

创建动态数组仅显示最后一个

jQuery推送到多维数组只返回最后一个值

使用jQuery查找表的最后一个输入值时遇到麻烦,我的函数仅返回乱码。如何获得最后一行的输入值?

使用<iframe>时,foreach循环仅返回一个值

jQuery each()仅选择最后一个值

jQuery .html仅获取JSON的最后一个值

如何使用for循环返回列表的最后一个元素

如何使用jQuery从数组对象中查找最新或最后一个元素值

使用猫鼬返回另一个对象数组中的最后一项

VBA数组循环未使用最后一个元素

使用数组删除for循环中的最后一个逗号

使用Geocoder()时,MarkerWithLabel仅显示google map API中的最后一个值

For 循环总是使用最后一个值 JavaScript - VueJS

使用另一个数组中的值返回一个数组中的数组

使用 firebase recyclerview 总是返回最后一个值

如何在导轨中仅使用一个for循环同时遍历两个数组