首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有循环的SASS生成变量

具有循环的SASS生成变量
EN

Stack Overflow用户
提问于 2018-06-07 09:09:50
回答 1查看 2.6K关注 0票数 1

TL;DR

SASS函数可以用来生成SASS变量吗?我的意图是采取一组小的颜色,并将它们加工成许多色调和色彩的这些颜色。

问题是:

N.B.我用https://github.com/at-import/color-schemer作为颜色函数。

我有以下变量:

代码语言:javascript
复制
$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如下所示):

代码语言:javascript
复制
$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;
  • 对于1-9 ($1):生成变量$#{$color}-0$1: shade($red-10, (100 - ($1 * 10)))
  • 对于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颜色,所以我想我必须创建动态列表?这可能吗?

我知道我正在使用一个变量来创建一个变量($#{$color}),并试图在循环中输出一个函数,并且不确定这在SASS中是否可行。

如果没有,是否有更好的工作流来处理这类事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 13:03:47

Currentry Sass不允许动态访问或创建变量。要做到这一点,唯一的办法就是使用地图。而且,如果您有一个可变的颜色数,那么您可以使用一个映射,其中每个键(颜色名称)都包含所有生成的颜色的列表,这样您就可以跟踪它们。

Note I只为测试目的更改了颜色函数和值(将shade更改为lighten,将tint更改为darken;在tint循环中,我向值添加了一个- 100,以将暗值保持在1到100之间)。你应该用你的代替那些功能。

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
.foo {
    color: get-color(blue, 5);
}

您可以检查工作示例这里

希望能帮上忙。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50737328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档