循环访问SCSS中的两个@Each列表 [英] Looping through two @each lists in SCSS

查看:26
本文介绍了循环访问SCSS中的两个@Each列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CSS中,我必须创建引用"头发颜色"和"发型"的类

我已经编写了一个Mixin来帮助提高CSS编写效率:

@mixin hair($hair, $colour) {
    .hair-#{$colour} .#{$hair}  { 
background:image-url("xsppsfhair#{$hair}#{$colour}.svg") center top no-repeat, 
image-url("xsppsheadgrey.svg") center top no-repeat, 
image-url("xsppsbhair#{$hair}#{$colour}.svg") center top no-repeat;
}


}

@include hair(spikyboy, blonde);    
@include hair(curlyafroboy, blonde);    

这将生成以下CSS

.hair-blonde .spikyboy {
  background: url('../images/xsppsfhairspikyboyblonde.svg?1348681869') center top no-repeat, url('../images/xsppsheadgrey.svg?1348834673') center top no-repeat, url('../images/xsppsbhairspikyboyblonde.svg?1348682005') center top no-repeat;
}

.hair-blonde .curlyafro {
  background: url('../images/xsppsfhaircurlyafroblonde.svg?1348681869') center top no-repeat, url('../images/xsppsheadgrey.svg?1348834673') center top no-repeat, url('../images/xsppsbhaircurlyafroblonde.svg?1348682005') center top no-repeat;
}

这很棒,但是因为我总共有35种发色和发型,所以最终@Includes还是太多了。

On this page,它说sass有一个@each,可用于遍历列表。还有另一个示例here。但是,这两个示例仅显示了循环通过1个列表。是否可以循环访问两个列表?

我已经尝试了我的代码的许多变体,但似乎都不起作用。我原以为以下方法肯定可以工作,但我只是收到一条错误消息,提示$COLOR未定义。

$hairstyle: (red, blonde, ltbrown);
$haircolour: (red, blonde, ltbrown);

    @each $hair in $haircolour, $colour in $haircolour {
    .hair-#{$colour} .#{$hair}  {
background:image-url("xsppsfhair#{$hair}#{$colour}.svg") center top no-repeat, 
image-url("xsppsheadgrey.svg") center top no-repeat, 
image-url("xsppsbhair#{$hair}#{$colour}.svg") center top no-repeat;
    }

    .girl.hair-#{$colour} #eyelash {
        background: image-url("xsppseyelash#{$colour}.svg") center top no-repeat;
    }
}

有没有人能给我指点一下我做错了什么?

推荐答案

您需要做的是在第一个循环内创建一个循环(我稍微简化了一点,这样更容易看到):

@mixin style-matrix($colors, $styles) {
    @each $s in $styles {
        @each $c in $colors {
            .#{$s} .#{$c} {
                background:image-url("xsppsfhair-#{$s}-#{$c}.svg");
            }
        }
    }
}

$colors: blonde, brunette, auburn;
$styles: beehive, pixie, bob;
@include style-matrix($colors, $styles);

您将获得以下输出:

.beehive .blonde {
  background: image-url("xsppsfhair-beehive-blonde.svg");
}

.beehive .brunette {
  background: image-url("xsppsfhair-beehive-brunette.svg");
}

.beehive .auburn {
  background: image-url("xsppsfhair-beehive-auburn.svg");
}

.pixie .blonde {
  background: image-url("xsppsfhair-pixie-blonde.svg");
}

.pixie .brunette {
  background: image-url("xsppsfhair-pixie-brunette.svg");
}

.pixie .auburn {
  background: image-url("xsppsfhair-pixie-auburn.svg");
}

.bob .blonde {
  background: image-url("xsppsfhair-bob-blonde.svg");
}

.bob .brunette {
  background: image-url("xsppsfhair-bob-brunette.svg");
}

.bob .auburn {
  background: image-url("xsppsfhair-bob-auburn.svg");
}

这篇关于循环访问SCSS中的两个@Each列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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