2013-03-18 97 views
11

可能有三種顏色的列表:騎自行車穿過一系列帶有sass的顏色

$ color-list:x y z;

然後通過循環應用這三種顏色並將它們添加到無序列表項上。

我想:

<li>row 1</li> (gets color x) 
<li>row 2</li> (gets color y) 
<li>row 3</li> (gets color z) 
<li>row 4</li> (gets color x) 

等等等等。

我曾嘗試使用@each(http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#each-directive)函數,但它只是在第一次通過列表後停止應用顏色。我希望顏色能夠保持騎行,直到列表中的項目用完爲止。

這是可能與sass?

+0

似乎是[第n個孩子(的情況下http://css-tricks.com/這將任意數量的顏色搭配如何-第n個孩子,工程/)。 Sass不知道你的標記。你可以用'@ each'遍歷列表項,但是使用'nnth-child'會更加靈活。 – steveax 2013-03-18 04:57:17

回答

31

如果它可能與純CSS,它可能與薩斯。

http://codepen.io/cimmanon/pen/yoCDG

$colors: red, orange, yellow, green, blue, purple; 

@for $i from 1 through length($colors) { 
    li:nth-child(#{length($colors)}n+#{$i}) { 
     background: nth($colors, $i) 
    } 
} 

輸出:

li:nth-child(6n+1) { 
    background: red; 
} 

li:nth-child(6n+2) { 
    background: orange; 
} 

li:nth-child(6n+3) { 
    background: yellow; 
} 

li:nth-child(6n+4) { 
    background: green; 
} 

li:nth-child(6n+5) { 
    background: blue; 
} 

li:nth-child(6n+6) { 
    background: purple; 
} 
+0

我明白這種方法......但我想要的是讓它不斷循環遍歷顏色。因此,在你的例子中,第七李將以紅色開始,直到所有李元素都被設計爲止。 – JoshuaRule 2013-03-18 14:16:03

+3

它呢?演示不會顯示嗎?這就是'6n + 1'所做的:'(6 * n)+ 1' = 1,7,13,19,25等。 – cimmanon 2013-03-18 14:24:55

+0

我修改了我之前的評論。這很好用!我明白你現在的工作方式。 – JoshuaRule 2013-03-18 16:02:03