当另一个变量为 true 时,LESS CSS 定义变量

Sme*_*ger 4 css less

我在 CSS 中的其他地方使用了保护表达式来实现 LESS 中的 IF 语句,但是当我尝试像这样声明变量时,这些对我不起作用......

@neutral: false;
@warm: true;

when (@neutral = true) {
    @green: #91C95B;
    @red: #F15647;
    etc...
}
when (@warm = true) {
    @green: #91AD3C;
    @red: #BF2A23;
    etc...
}
Run Code Online (Sandbox Code Playgroud)

这是我希望如何使用该变量的示例

h1 {
    color:@green;
}
Run Code Online (Sandbox Code Playgroud)

这就是我期望它编译为 CSS 的方式

h1 {
    color: #91AD3C;
}
Run Code Online (Sandbox Code Playgroud)

这可以用 LESS 实现吗?还是我需要修改我的代码才能使用 mixin 防护?

Jof*_*heo 5

Guarded Mixins你可以这样使用:

@neutral: false;
@warm: true;

.color() when (@neutral) {
    @green: #91C95B;
}
.color() when (@warm) {
    @green: #91AD3C;
}
.color();


h1 {
    color:@green;
}
Run Code Online (Sandbox Code Playgroud)

  • > 我认为 LESS 文档可以做一些澄清 - 该文档实际上对守卫的内容非常详细([Mixin Guards](http://lesscss.org/features/#mixin-guards-feature),[CSS Guards] (http://lesscss.org/features/#css-guards-feature))。我想您缺少的实际上是变量作用域信息,它确实没有完全记录(除了两个小部分:[Scope](http://lesscss.org/features/#features-overview-feature-scope)、[Mixins作为函数](http://lesscss.org/features/#mixins-as-functions-feature))。 (2认同)