简体   繁体   English

根据JavaScript中的数组更改div背景颜色?

[英]Change div background colors according to array in JavaScript?

I have divs that are loaded from a database, the number of divs is not known ( may increase or decrease ) , each div have a random color from my code : 我有从数据库中加载的div,div的数量未知(可能会增加或减少),每个div从我的代码中都有随机的颜色:

$(".ooicon").each(function() {
  var items = ["#9062aa","#3fb4e9","#6fc063","#d94949","#f8951e","#7a564a","#029688","#2d2f79","#e81f63"];
  var color = items[Math.floor(Math.random() * items.length)];
  $(this).css('background', color);
});

this code gives a random color changed on each reload or refresh , I want to make the colors static and not changed on refresh, 这段代码给出了每次重新加载或刷新时都会更改的随机颜色,我想使颜色为静态,而在刷新时不更改,

for example the first div will have the color #9062aa from the code, the second will be #3fb4e9 and so on.. when the colors in the array reach the last, it start over again with the first color. 例如,第一个div的代码颜色为#9062aa ,第二个div的颜色为#3fb4e9 ,依此类推。等等。.当数组中的颜色达到最后一个颜色时,它从第一个颜色重新开始。

I hope you understand me. 我希望你能理解我。

You just have to loop over the array without using random, and when the index is equal to the array items, reset it to 0. 您只需要在不使用随机数的情况下循环遍历数组,并且当索引等于数组项时,将其重置为0。

See below snippet: 请参见以下代码段:

 var items = ["#9062aa", "#3fb4e9", "#6fc063", "#d94949", "#f8951e", "#7a564a", "#029688", "#2d2f79", "#e81f63"]; var index = 0; var color; $(".ooicon").each(function() { if (index == items.length) index = 0; color = items[index]; $(this).css('background', color); index++; }); 
 .ooicon { width: 100%; height: 10px; margin-bottom: 3px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> <div class="ooicon"></div> 

The colours change as you're using Math.random() . 颜色随着您使用Math.random()变化而变化。 If you want them colours to be static based on the position of the elements in the set, use their index value instead. 如果您希望它们基于集合中元素的位置的颜色是静态的,请改用它们的索引值。 This can be retrieved as the first argument provided to each() : 可以将其作为提供给each()的第一个参数进行检索:

var items = ["#9062aa", "#3fb4e9", "#6fc063", "#d94949", "#f8951e", "#7a564a", "#029688", "#2d2f79", "#e81f63"];

$(".ooicon").each(function(i) {
  var color = items[i % items.length)];
  $(this).css('background-color', color);
});

Note that I moved the items declaration outside of the loop; 请注意,我将items声明移到了循环之外 there's no point redefining the same data in each iteration. 在每次迭代中都没有必要重新定义相同的数据。 Also note the use of the modulo operator ( % ) to simplify the iterative access of the array, and the change to set background-color directly. 还要注意,使用模运算符( % )可以简化数组的迭代访问,也可以更改为直接设置background-color

You can do that without using math.random, check below snippet : 您可以在不使用math.random的情况下执行此操作,请检查以下代码段:

 var colors = ["#9062aa","#3fb4e9","#6fc063","#d94949","#f8951e","#7a564a","#029688","#2d2f79","#e81f63"]; var count = 0; function bgcolor() { $("body").find("div").each(function() { var index = $("body").find("div").index(this); var i = (index + count) % colors.length; $(this).css("background-color", colors[i]); }); count++; } $(window).resize(bgcolor); $(document).ready(bgcolor); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Content -1</div> <div>Content - 2</div> <div>Content - 3</div> <div>Content - 4</div> <div>Content - 5</div> <div>Content - 6</div> <div>Content - 7</div> <div>Content - 8</div> <div>Content - 9</div> <div>Content - 10</div> <div>Content - 11</div> <div>Content - 12</div> <div>Content - 13</div> 

DO not keep your colors array in .each loop, dont seem to be good code. 不要将颜色数组保留在.each循环中,这似乎不是很好的代码。 so if you divide current index of item in .each loop with length, you will get desired reset logic to chose color from array. 因此,如果在.each循环中将项目的当前索引除以长度,则将获得所需的复位逻辑以从数组中选择颜色。

 var items = ["#9062aa","#3fb4e9","#6fc063","#d94949","#f8951e",
 "#7a564a","#029688","#2d2f79","#e81f63"];

 $(".ooicon").each(function(index) {

   var color = items[index % items.length];
   $(this).css('background', color);

  });

This should work,hope this helps. 这应该可以,希望有帮助。

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

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