在Sass中动态创建或引用变量我试图在变量上使用字符串插值来引用另一个变量:// Set up variable and mixin$foo-baz: 20px;@mixin do-this($bar) {
width: $foo-#{$bar};}// Use mixin by passing 'baz' string as a param for use $foo-baz variable in the mixin@include do-this('baz');但是,当我这样做时,我会得到以下错误:未定义变量:“$foo-”。Sass支持PHP风格的变量吗?
3 回答
繁星coding
TA贡献1797条经验 获得超4个赞
$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; }
千万里不及你
TA贡献1784条经验 获得超9个赞
$foo: 2em; $bar: 1.5em; @function foo-or-bar($value) { @if $value == "foo" { @return $foo; } @else { @return $bar; } } @mixin do-this($thing) { width: foo-or-bar($thing); }
添加回答
举报
0/150
提交
取消