在循环内混合两个SASS变量

ste*_*anz 1 css sass compass-sass

我有几个scss变量看起来像:

$box-bg1: #7a8360;
$box-bg2: #918261;
$box-bg3: #6a8177;
$box-bg4: #686f5e;
Run Code Online (Sandbox Code Playgroud)

我想在for中使用这些变量,例如:

@for $i from 1 through 4 {
  .locationBox:nth-child(#{$i}) { background: $box-bg#{$i}; }
}
Run Code Online (Sandbox Code Playgroud)

编译代码后我得到这个错误:语法错误:未定义的变量:"$ box-bg",看起来像#{$i}变量对我的代码没有任何意义.有没有办法在这样的循环中使用变量?

注意:我也使用指南针

cim*_*non 5

Sass不支持变量变量.要以编程方式创建选择器,您必须使用列表执行此操作:

$box-bg: #7a8360, #918261, #6a8177, #686f5e;

@for $i from 1 through length($box-bg) {
  .locationBox:nth-child(#{$i}) { background: nth($box-bg, $i); }
}
Run Code Online (Sandbox Code Playgroud)

输出:

.locationBox:nth-child(1) {
  background: #7a8360;
}

.locationBox:nth-child(2) {
  background: #918261;
}

.locationBox:nth-child(3) {
  background: #6a8177;
}

.locationBox:nth-child(4) {
  background: #686f5e;
}
Run Code Online (Sandbox Code Playgroud)