【发布时间】:2009-12-14 21:35:44
【问题描述】:
想知道是否可以在 Sass 中使用数组,因为我发现自己在重复以下类型的事情:
.donkey
h2
background-color= !donkey
.giraffe
h2
background-color= !giraffe
.iguana
h2
background-color= !iguana
【问题讨论】:
想知道是否可以在 Sass 中使用数组,因为我发现自己在重复以下类型的事情:
.donkey
h2
background-color= !donkey
.giraffe
h2
background-color= !giraffe
.iguana
h2
background-color= !iguana
【问题讨论】:
绝对的。
$animals: "donkey", "giraffe", "iguana"
@foreach $animal in $animals
.#{$animal}
h2
background-color: !#{$animal}
【讨论】:
不,这是不可能的。最好的方法是定义一个 mixin:
+animal-h2(!name, !color)
.#{name} h2
background-color= !color
那么你可以每个样式只有一行,而不是三行:
+animal-h2("donkey", !donkey)
+animal-h2("giraffe", !giraffe)
+animal-h2("iguana", !iguana)
【讨论】:
nex3 的回答是正确的。为了让它与 SASS on Rails 3.1 一起使用,我需要在 css.scss 文件中包含以下内容:
$donkey: #CC4499;
@mixin animal-h2($name, $color) {
.#{$name} h2 {
background-color: $color;
}
}
@include animal-h2("donkey", $donkey);
@include animal-h2("horse", #000);
哪个输出:
.donkey h2 {
background-color: #CC4499;
}
.horse h2 {
background-color: black;
}
【讨论】: