Is it possible to use variable names dynamically in SASS each loop? [duplicate]

我们两清 提交于 2019-12-30 08:03:44

问题


This is a simplified example. What I'd like to do is use the items in the array to output variable values I've previously created. The syntax below that tries create a variable by concatenating a '$' is obviously wrong but I'm using it to make it clear what I'm trying to do.

$puma-width: 100px;
$slug-width: 200px;

@each $animal in puma, slug {
  .#{$animal}-title {
    width: $+#{$animal}-width;
  }
}

Desired output:

.puma-title {
   width: 100px;
}
.slug-title {
   width: 200px;
}

回答1:


That's something that I wanted in SASS as well, but after reading their discussion list, I concluded that this kind of variable interpolation is not supported yet.

I didn't try Less, but their documentation suggests it would be possible with @@ syntax.




回答2:


The alternative is to pass a map instead of item list for the @each directive. The following will work:

@each $animal, $width in (puma, 100px),
                         (slug, 200px) {
  .#{$animal}-title {
    width: #{$width};
  }
}


来源:https://stackoverflow.com/questions/7209031/is-it-possible-to-use-variable-names-dynamically-in-sass-each-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!