【问题标题】:Mix two SASS variables inside a loop [duplicate]在循环中混合两个 SASS 变量
【发布时间】:2013-03-31 03:59:27
【问题描述】:

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

$box-bg1: #7a8360;
$box-bg2: #918261;
$box-bg3: #6a8177;
$box-bg4: #686f5e;

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

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

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

注意:我也使用指南针

【问题讨论】:

    标签: css sass compass-sass


    【解决方案1】:

    Sass 不支持可变变量。要以编程方式创建选择器,您必须使用列表:

    $box-bg: #7a8360, #918261, #6a8177, #686f5e;
    
    @for $i from 1 through length($box-bg) {
      .locationBox:nth-child(#{$i}) { background: nth($box-bg, $i); }
    }
    

    输出:

    .locationBox:nth-child(1) {
      background: #7a8360;
    }
    
    .locationBox:nth-child(2) {
      background: #918261;
    }
    
    .locationBox:nth-child(3) {
      background: #6a8177;
    }
    
    .locationBox:nth-child(4) {
      background: #686f5e;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2017-03-07
      相关资源
      最近更新 更多