简体   繁体   English

Sass:在SCSS中使用两个@each列表

[英]Sass: Using two @each lists in SCSS

In my CSS I have to create classes that make reference to a 'hair colour' and 'hairstyle' 在我的CSS中,我必须创建引用“头发颜色”和“发型”的类

I have written a mixin to help make my CSS writing more efficient: 我写了一个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);    

This produces the following CSS 这将产生以下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;
}

This is great, but as I have a total combination of 35 hair colours and hair styles, I still end up with way too many @includes. 很棒,但是由于我总共拥有35种头发颜色和发型的组合,因此我仍然会遇到太多@includes的情况。

On this page , it says SASS has an @each which can be used to loop through lists. 在此页面上 ,它说SASS有一个@each,可用于循环浏览列表。 There is a further example here . 有一个进一步的例子在这里 However, both these examples only show looping through 1 list. 但是,这两个示例仅显示了循环遍历1个列表。 Is it possible to loop through two lists? 是否可以遍历两个列表?

I have tried many variations of my code, but none seem to work. 我已经尝试了许多代码变体,但是似乎没有一个奏效。 I thought the following would definitely work, but I just get an error message about $colour being undefined. 我以为以下内容肯定可以工作,但是我只是收到一条有关$ colour未定义的错误消息。

$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;
    }
}

Could someone please give me some pointers as to what I am doing wrong? 有人可以给我一些有关我做错事情的提示吗?

What you need to do is create a loop inside the first loop (I've simplified a bit so it's easier to see): 您需要做的是在第一个循环中创建一个循环(我已经简化了一点,因此更容易看到):

@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);

You get this output: 您得到以下输出:

.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");
}

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

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