這實際上是可以用來做SASS映射代替的變量。下面是一個簡單的例子:
動態引用:
$colors: (
blue: #007dc6,
blue-hover: #3da1e0
);
@mixin colorSet($colorName) {
color: map-get($colors, $colorName);
&:hover {
color: map-get($colors, $colorName#{-hover});
}
}
a {
@include colorSet(blue);
}
輸出爲:
a { color:#007dc6 }
a:hover { color:#3da1e0 }
動態創建:
@function addColorSet($colorName, $colorValue, $colorHoverValue: null) {
$colorHoverValue: if($colorHoverValue == null, darken($colorValue, 10%), $colorHoverValue);
$colors: map-merge($colors, (
$colorName: $colorValue,
$colorName#{-hover}: $colorHoverValue
));
@return $colors;
}
@each $color in blue, red {
@if not map-has-key($colors, $color) {
$colors: addColorSet($color, $color);
}
a {
&.#{$color} { @include colorSet($color); }
}
}
輸出爲:
a.blue { color: #007dc6; }
a.blue:hover { color: #3da1e0; }
a.red { color: red; }
a.red:hover { color: #cc0000; }
@castus這是如何解決您的問題?我遇到了一個非常類似的問題,我需要從列表中取一個字符串值並添加一個$並將其用作變量。 – cmegown 2013-09-11 19:52:25