如何在 Sass 的循环中从mixin argument
获取array variable
?
示例代码如下:
$colors: red green blue;
$red-foo: 100px;
$red-bar: 110px;
$red-baz: 120px;
$green-foo: 100px;
$green-bar: 110px;
$green-baz: 120px;
$blue-foo: 100px;
$blue-bar: 110px;
$blue-baz: 120px;
@mixin item($color-foo, $color-bar, $color-baz){
width: $color-foo,;
height: $color-bar;
...
}
@each $color in $colors {
.class-#{$color}{
@include item($color#{-foo}, $color#{-bar}, $color#{-baz})
}
}

所需的输出如下:
.class-red{
//variables
}
.class-green{
//variables
}
.class-blue{
//variables
}

还有一种方法可以简化$color-foo, $color-bar, $color-baz
@include item($color-shorthand)
的缩写吗?也许可以有更好的解决方案。
答案 0 :(得分:5)
你想要Sass地图
$colors: (
red: (
foo: 100px,
bar: 110px,
baz: 110px
),
green: (
foo: 200px,
bar: 210px,
baz: 220px
),
blue: (
foo: 300px,
bar: 310px,
baz: 320px
)
);
@mixin item ($color-foo, $color-bar, $color-baz) {
width: $color-foo;
height: $color-bar;
padding: $color-baz;
}
@each $name, $value in $colors {
.class-#{$name} {
@include item(
map-get($value, foo),
map-get($value, bar),
map-get($value, baz)
);
}
}