如何分离scss函数返回值

How to separate scss function returned values

我有两个 SCSS 映射调用 $my-map-1$my-map-2。每个地图都有带有十六进制值的键。我分别写了一个函数来 return 每个映射的键和十六进制值 ($key, $value)。

之后,我用我的函数写了一个@if条件来检查地图。我将地图名称传递给函数。如果在那里映射,请检查 $key 是否等于给定名称。如果是,请将 $key$valu 传递给我的颜色混合。这是我的代码。

$my-map-1: (
        map-1-color-1: #506c89,
        map-1-color-2: #737373,
        map-1-color-3: #2a3949,
        map-1-color-4: #182028,
);

$my-map-2: (
        map-2-color-1: #fff,
        map-2-color-2: #000,
        map-2-color-3: #ddd,
        map-2-color-4: #ccc,
);

//the function to read te map and return the key and the value.
@function color-map($color-map) {
  @each $key, $value in $color-map {    
    @return ($key, $value);
  }
}

//mixin
@mixin color-mix($color){
  color: $color;
}

//css classes from here
@if color-map($my-map-1) {
  if($key == map-1-color-1) {
    .my-class{
      @include color-mix($color:$value);
    }
  }
}

可以用map-get方法,很有用:http://sass-lang.com/documentation/Sass/Script/Functions.html#map_get-instance_method

这是mixin的一个例子。我也将您的密钥作为参数传递:也许它更好,因为如果需要,您还可以检查其他密钥名称:

$my-map-1: (
  map-1-color-1: #506c89,
  map-1-color-2: #737373,
  map-1-color-3: #2a3949,
  map-1-color-4: #182028
);

$my-map-2: (
  map-2-color-1: #fff,
  map-2-color-2: #000,
  map-2-color-3: #ddd,
  map-2-color-4: #ccc
);


@mixin color-map($color-map, $key-map) {
  @each $key, $value in $color-map {    
    @if($key == $key-map) {
      .my-class{
        color: map-get($color-map, $key);
      }
    }
  }
}

@include color-map($my-map-1, map-1-color-1); 

您的输出将是:

.my-class {
  color: #506c89;
}