首页 文章

SASS - 使用循环生成变量

提问于
浏览
1

TL; DR

SASS功能可以用于生成SASS变量吗?我的目的是采用一小组颜色并将它们加工成许多色调和这些颜色的色调 .

问题:

N.B. 我正在使用https://github.com/at-import/color-schemer作为颜色功能 .

我有以下变量:

$root-color:    hsl(10, 85%, 45%);

$red:        $root-color;
$vermilion:  ryb-adjust-hue($red, 30);
$orange:     ryb-adjust-hue($red, 60);
$amber:      ryb-adjust-hue($red, 90);
$yellow:     ryb-adjust-hue($red, 120);
$chartreuse: ryb-adjust-hue($red, 150);
$green:      ryb-adjust-hue($red, 180);
$teal:       ryb-adjust-hue($red, 210);
$blue:       ryb-adjust-hue($red, 240);
$violet:     ryb-adjust-hue($red, 270);
$purple:     ryb-adjust-hue($red, 300);
$magenta:    ryb-adjust-hue($red, 330);

对于每种颜色,我正在尝试生成以下输出( $red 如下所示):

$red-10: $red;

$red-01: shade($red-10, 90);
$red-02: shade($red-10, 80);
$red-03: shade($red-10, 70);
$red-04: shade($red-10, 60);
$red-05: shade($red-10, 50);
$red-06: shade($red-10, 40);
$red-07: shade($red-10, 30);
$red-08: shade($red-10, 20);
$red-09: shade($red-10, 10);

$red-11: tint($red-10, 10);
$red-12: tint($red-10, 20);
$red-13: tint($red-10, 30);
$red-14: tint($red-10, 40);
$red-15: tint($red-10, 50);
$red-16: tint($red-10, 60);
$red-17: tint($red-10, 70);
$red-18: tint($red-10, 80);
$red-19: tint($red-10, 90);

我可以将其归结为以下内容:

对于每个 $color

  • $red-10: $red;

  • For 1 - 9 ($1) :生成变量 $#{$color}-0$1: shade($red-10, (100 - ($1 * 10)))

  • For 1 - 9 ($1) :生成变量 $#{$color}-($1 * 10): tint($red-10, ($1 * 10))

显然我收到一个错误:

Error: Invalid CSS after "...m 1 through 9 {": expected 1 selector or at-rule, was "#{$color}-$1: shade"

我已经研究过使用列表和 @append ,但由于我有x种颜色,我必须创建动态列表吗?这有可能吗?

我'm aware that I' m都使用变量来创建变量( $#{$color} )并尝试在循环中输出一个函数,并且我不确定这是否可能与SASS一起使用 .

如果没有,这种事情有更好的工作流程吗?

1 回答

  • 2

    Currentry Sass不允许动态访问或创建变量 . 实现这一目标的唯一方法是使用 Map . 并且,如果您有可变数量的颜色,那么您可以使用一个 Map ,其中每个键(颜色名称)包含所有生成颜色的列表,因此您可以跟踪它们 .

    Note 我更改了颜色函数和值仅用于测试目的(将 shade 更改为 lightentint 更改为 darken ;同样在 tint 循环中,我将 - 100 添加到该值,以使变暗值保持在1和100之间) . 你应该用你的功能替换这些功能 .

    // Colors to use
    $colors:(
        red:  red,
        blue: blue
    );
    
    // Generated color lists
    $generated: ();
    
    // For each defined color
    @each $name, $color in $colors {
    
        // Current color map
        $current : ();
    
        // Generates the colors transformations (shades)
        @for $i from 1 through 9 {
            $current: append($current, lighten($color, (100 - ($i * 10))));
        }
    
        // Adds the current color
        $current: append($current, $color);
    
        // Generates the colors transformations (tints)
        @for $i from 11 through 19 {
            $current: append($current, darken($color, ($i * 10) - 100));
        }
    
        // If the map has not been created
        @if map-has-key($generated, $name) == false {
            $generated: map-merge($generated, ($color : $current));
        }
    
    }
    
    // Create a function to get the desired color
    @function get-color($color, $index: 10) { // Default to base color
    
        // Get the desired color map
        $list: map-get($generated, $color);
    
        // Return the color index
        @return nth($list, $index);
    
    }
    

    然后你可以使用创建的函数来获得你选择的颜色idex:

    .foo {
        color: get-color(blue, 5);
    }
    

    您可以查看工作示例here .

    希望能帮助到你 .

相关问题