简体   繁体   English

SASS / SCSS @每个多个数组

[英]SASS/SCSS @each multiple arrays

I am trying to write a sass mixing using the values from two arrays to output my button classes. 我正在尝试使用两个数组中的值编写一个混蛋来输出我的按钮类。 Not sure if what I am trying to do is possible at all. 不知道我想做的事是否可能。

So I have two arrays: 所以我有两个数组:

$buttonNames: ('black', 'primary', 'red', 'green', 'orange');
$buttonColors:(black, blue, red, green, orange);

and then my mixin is: 然后我的mixin是:

@mixin underlineButton($class, $name, $size, $color: black) {
    .#{$class}-underline-#{$name} {
        background-color: transparent;
        border-bottom: $size + px solid $color;
        border-radius: 0;
        font-size: .75rem;
    }
}

and then I do an @each loop for the names, and attempted to nest another loop inside this to get the colors. 然后对名称进行@each循环,并尝试在其中嵌套另一个循环以获取颜色。 Obviously this isn't working! 显然这是行不通的! Just wondering if it is even possible. 只是想知道是否有可能。

@each $name in $buttonNames {
    @each $color in $buttonColors {
        @include underlineButton('btn', $name, 3, $color)
    }
}

The desired output would be something like: 所需的输出如下所示:

.btn-underline-black {
    background-color: transparent;
    border-bottom: 3px solid black;
    border-radius: 0;
    font-size: .75rem;
}

// .btn-underline-* for the rest of the matching keys and colors

Here's a DEMO 这是一个演示

If you need to keep your values separate, in 2 lists, then you can... 如果您需要将值分开保存在2个列表中,则可以...

// loop over $buttonNames
@each $buttonName in $buttonNames {
  // Find the current index of $buttonNames...
  $index: index($buttonNames, $buttonName);
                                  // ... to pull the right from $buttonColors
  @include underlineButton('btn', $buttonName, 3, nth($buttonColors, $index));
}

However, using a map is a little easier. 但是,使用地图要容易一些。

$buttons: (
  'black': black,
  'primary': blue,
  'red': red,
  'green': green
);

@each $buttonName, $color in $buttons  {
    @include underlineButton('btn', $buttonName, 3, $color)
}
$buttons:   ('black', $black),
            ('primary', $primary),
            ('red', $red),
            ('green', $green);

@each $buttonName, $color in $buttons  {
    @include underlineButton('btn', $buttonName, 3, $color)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM