使用数组值作为变量 [英] Use array value as variable

查看:30
本文介绍了使用数组值作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这些颜色:

$everlastingGreeN: #232553;
$vividRed: #3435454;
// and many more
//and define those:
$colors: (everlastingGreeN, vividRed);

如何迭代这些值,以便可以使用颜色名称作为类名,然后使用值作为背景颜色?

How do I iterate those values so I can use the color name as the class name then the value as the background color?

@each $colors in $colors{
    &.#{$colors} {
        background: {color value};
        &:hover {
            background: darken({color value}, 10%);
        }
    }
}

推荐答案

您要查找的是列表列表.

What you're looking for is a list of lists.

$everlastingGreeN: #232553;
$vividRed: #343545;
// and many more
//and define those:
$colors: everlastingGreeN $everlastingGreeN, vividRed $vividRed;

@each $color in $colors {
    &.#{nth($color, 1)} {
        background: nth($color, 2);
        &:hover {
            background: darken(nth($color, 2), 10%);
        }
    }
}

这篇关于使用数组值作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆