简体   繁体   English

Div循环中的背景

[英]Div background within while loop

when using (count/2) for giving background color in div, it applied for alternative boxes. 当使用(count / 2)在div中给出背景颜色时,它适用于替代盒子。 But i want to give div background for first three boxes same color then for the next three boxes diff color then again for the next three boxes the first color then next three boxes the second color. 但我想给前三个框相同颜色的div背景,然后为接下来的三个框差异颜色然后再次为接下来的三个框第一个颜色然后接下来三个框第二个颜色。

The alternative color should be applied for three boxes once. 替代颜色应该应用于三个盒子一次。 Please clarify this one 请澄清一下

You can use modulo 你可以使用modulo

var c;
if (count % 6 < 3)
  // color one
  c = '#fff';
else
  // color two
  c = '#ccc';

Or as a one-liner 或者作为一个班轮

var c = count % 6 < 3 ? '#fff' : '#ccc';

Try something like 尝试类似的东西

k=0, Color=Red

while()
{
    if(k == 3)
    {
      k=0;
     if(Color == Red) Color=Green
     else Color = Red

    }
    else
   {
     k++;
   }
}

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

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