4

有没有办法将外部变量导入mixins?
我想从 mixin 参数生成一个变量名
,然后从外部源调用它。这有道理吗?


变量.scss

/* Striped status bar variables ******************************************/

$greyFirstGradientStartColor: #999999;
$greyFirstGradientEndColor: #d3cfcf;
$greySecondGradientStartColor: #ababab;
$greySecondGradientEndColor: #595959;

mixins.scss

@import variables.scss

[...]

@mixin gradient-repeating($color, $deg, $firstWidth, $space, $secondWidth){

  background-image:
    repeating-linear-gradient(
      $deg,
      $(#{$color}FirstGradientStartColor),
      $(#{$color}FirstGradientEndColor) $firstWidth+px,
      $(#{$color}SecondGradientStartColor) ($firstWidth+$space)+px,
      $(#{$color}SecondGradientStartColor) $secondWidth+px
  );
}

我的主css文件.scss

@import variables.scss;  
@import mixins.scss;  

[...]

@include gradient-repeating(grey, -45, 20, 0, 20);  
4

1 回答 1

2

不,Sass 中不存在变量变量。通常改为使用列表或列表列表。

你的 mixin 可以这样写:

$grey-gradient: #999999 #d3cfcf, #ababab #595959;

@mixin gradient-repeating($color, $deg, $firstWidth, $space, $secondWidth){
    $firstColor: nth($color, 1);
    $secondColor: nth($color, 2);
    background-image:
        repeating-linear-gradient(
          $deg,
          nth($firstColor, 1),
          nth($firstColor, 2) $firstWidth+px,
          nth($secondColor, 1) ($firstWidth+$space)+px,
          nth($secondColor, 1) $secondWidth+px
        );
}

.foo {
    @include gradient-repeating($grey-gradient, -45, 20, 0, 20);
}
于 2013-01-29T13:59:16.003 回答