css 从动态生成的索引开始循环遍历数组

bqjvbblv  于 5个月前  发布在  其他
关注(0)|答案(3)|浏览(70)

抱歉,如果标题没有准确地抓住我的问题,我不知道如何写在一个句子。

问题描述:假设我有一个颜色数组和一堆div。我想在数组颜色中循环使用div背景色,但同时要确保每个div都从颜色数组中的不同位置开始。
**示例:**我的颜色数组是[red, blue, yellow, green]。第一个div颜色从red开始,然后在blueyellowgreen之间循环,最后返回到red。Div 2从blue开始,然后到yellow, green, red, blue,依此类推,直到div元素的个数为止。

我能想到的两种可能的解决方案是:
1.我是否必须为每个div生成一个新的颜色数组?(可能通过向计数器添加1并执行拼接、推送或弹出操作)
1.我是否可以在原始数组中循环,只从每个div的不同位置开始?(根据其位置动态生成偏移量-第一个div、第二个div、第三个div等)
这两种方法是否都是解决问题的可行方法?

6gpjuf90

6gpjuf901#

听起来你需要的只是取模。一旦你确定了一个特定元素的起始索引,就把索引取模数组长度。如果colors数组是colors,那么你可以这样做,

async function cycle(elm, index) {
  while (true) {
    elm.style.backgroundColor = colors[index];
    index = (index + 1) % colors.length;
    await delay(1000);
  }
}
cycle(someElement, 0);
// or
cycle(someOtherElement, 1);
// etc

字符串

pu3pd22g

pu3pd22g2#

只是为了好玩,这个动画在CSS。这个问题被标记为css

.color-change {
  --animation-duration: 5s;
  display: inline-block;
  height: 100px;
  width: 100px;
  animation: colors var(--animation-duration) steps(1, end) infinite;
}
.color-change:nth-child(4n+2) { animation-delay: calc(var(--animation-duration) * -.25) }
.color-change:nth-child(4n+3) { animation-delay: calc(var(--animation-duration) * -.5) }
.color-change:nth-child(4n+4) { animation-delay: calc(var(--animation-duration) * -.75) }

@keyframes colors {
  from,
  to  { background: red; }
  25% { background: blue; }
  50% { background: yellow; }
  75% { background: green; }
}

个字符

vwoqyblh

vwoqyblh3#

你不需要为每个div生成一个新的数组,所以虽然你提出的两个解决方案都可以工作,但第二个是更好的方法。
当为每个div设置初始颜色时,您只需根据当前div的索引选择颜色。
第一个月
由于div可以比颜色多,所以一旦div的索引超过颜色的数量,就需要从数组的开头开始。如CertainPerformance's answer所示,可以使用索引的模数轻松地将索引“循环”回到开头。因此赋值变为:
divs[index].style.backgroundColor = colors[index % colors.length];
一旦你完成了初始的颜色分配,一种遍历颜色的方法是简单地找到当前颜色的索引并递增1,再次使用模数来遍历颜色。

var currentColor = doc.style.backgroundColor;
var colorIndex = (colors.indexOf(currentColor) + 1) % colors.length;
doc.style.backgroundColor = colors[colorIndex];

字符串
下面是一个完整的例子。

var colors = ['red', 'blue', 'yellow', 'green', 'purple'];

function assignInitialColors() {
  var divs = document.getElementsByClassName("color-change");
  for (var index in divs) {
    var colorIndex = index % colors.length;
    if(divs[index].style) {
      divs[index].style.backgroundColor = colors[colorIndex];
    }
  }

  setInterval(iterateColors, 2000);
}

function iterateColors() {
   var divs = document.getElementsByClassName("color-change");
    for (var index in divs) {
    var doc = divs[index];
    var currentColor = doc.style && doc.style.backgroundColor;
    if (currentColor) {
      var colorIndex = (colors.indexOf(currentColor) + 1) % colors.length;
      doc.style.backgroundColor = colors[colorIndex];
    }
  }
}

assignInitialColors();
div, .color-change {
  min-height: 60px;
  min-width: 60px;
  margin: 5px;
  float: left;
}
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>
<div class="color-change"></div>

相关问题