【发布时间】:2014-02-21 19:53:52
【问题描述】:
在我们的应用中,我们有一个预设的颜色列表,用户可以从中进行选择,并且与该用户相关的所有内容都将具有该颜色。
在整个应用程序中,我们将拥有各种模块,并将颜色附加为类名。
例如。
<div class="example_module green">
...
</div>
我们在 CSS 中使用 LESS。
在很多地方,我们都在做这样的事情:
.example_module.green { background: @green; }
.example_module.red { background: @red; }
.example_module.blue { background: @blue; }
etc
我希望能够将所有这些颜色名称设置为一个数组并对其进行迭代。如果我们以后添加颜色,我们只需要在一个地方添加它。
伪代码:
@colors: ['green', 'red', 'blue'];
for each @color in @colors {
.example_module.@color { background: @color; }
}
LESS 是否也支持这样的功能?
【问题讨论】:
-
在 LESS 中可以使用递归。这在 SASS 中要简单得多。也许你听说过谷歌? ;^)
-
SASS 不是要走的路。我理解为什么它看起来更容易做,但从长远来看,LESS 或 LESS fork 会在完全遵守语法的情况下出现。