sass complie错误,提示$ color:即使我声明了color,null也不是颜色

时间:2018-08-22 10:55:03

标签: css sass

我是sass的新手,我写了一些sass代码,但未编译。

 $classes : primary secondary success warning danger;
    $colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);
    @each $class in $classes{
      .btn-#{$class}{
        $currentColor: map-get($colors,#{$class});
        background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
      }
    }

错误是:

$color: null is not a color.
stdin 14:55  root stylesheet on line 14 at column 55

但是当我用变量替换线性梯度时,它可以正常工作,即

$classes : primary secondary success warning danger;
$colors : (primary:#007bff,secondary : #6c757d,success: #28a745,warning: #ffc107,dangaer: #dc3545);

    @each $class in $classes{
      .btn-#{$class}{
        $currentColor: map-get($colors,#{$class});
        background:$currentColor;
        //background:linear-gradient(to right,$currentColor,lighten($currentColor,10%));
      }
    }

这是代码已成功编译。

linear-gradient()函数中$ currentColor变量的作用或作用是什么

1 个答案:

答案 0 :(得分:2)

实际上,有些东西可以将变量从map-get传递给其他sass函数。

但是您可以稍微修改一下代码,它将起作用:

$classes: primary secondary success warning danger;
$colors: (
    primary: ( normal: #007bff, light: lighten(#007bff,10%) ),
    secondary: ( normal: #6c757d, light: lighten(#6c757d,10%) ),
    success: ( normal: #28a745, light: lighten(#28a745,10%) ),
    warning: ( normal: #ffc107, light: lighten(#ffc107,10%) ),
    danger: ( normal: #dc3545, light: lighten(#dc3545,10%) )
);
@each $class in $classes{
  .btn-#{$class}{
    $currentColor: map-get(map-get($colors,#{$class}), normal);
    $currentColorLighten: map-get(map-get($colors,#{$class}), light);

    background: linear-gradient(to right, $currentColor, $currentColorLighten);
  }
}

您为每个类定义了两种颜色(普通版和浅色版),只需通过double map-get即可使用。