在 LessCSS 中,如何将 true 分配给变量?

发布于 2025-01-06 19:27:39 字数 551 浏览 2 评论 0原文

我是 Less 新手,我正在尝试实现简单的 mixin,但这会引发编译错误:

@show-image: true;

.testimg() when not (@show-image) {
    display: none;
}

img.test {
    .testimg;
}

编译器似乎不喜欢我的 @show-image: true 赋值。我尝试分配数字 1,但结果是

img.test {
    display: none;
}

,这是有道理的(根据 文档除关键字true之外的任何值都是假的

但是我可以将关键字 true 分配给我的 @show-image 变量吗?还是它只适用于参数?

I'm new to Less, and I'm trying to implement simple mixins, but this throws a compilation error:

@show-image: true;

.testimg() when not (@show-image) {
    display: none;
}

img.test {
    .testimg;
}

It seems the compiler doesn't like my @show-image: true assignation. I tried assigning the number 1, but the result was

img.test {
    display: none;
}

And that makes sense since (according to the documentation) any value other than the keyword true is falsy.

But can I assign the keyword true to my @show-image variable, or does it only work for with arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦断已成空 2025-01-13 19:27:39

Lucho,我同意你的观点......它还很年轻,编译器应该更具描述性,但这并不像你必须定义每个可能的条件(尽管在这种情况下,真/假变量似乎是首先的情况)...只需添加一个失败的“默认”情况,例如“switch”语句:

@show-image: false;

.testimg() when not (@show-image){
    display: none;
}

.testimg(){
    display: inherit;
}

img.test {
    .testimg;
}

Lucho, I'm agree with you... it is young and compiler should be a LOT more descriptive, but it is not like you have to define every possible condition (although in this case of a true/false variable it appears to be the case at first)... just have to add a "default" case where fall through, like a "switch" statement:

@show-image: false;

.testimg() when not (@show-image){
    display: none;
}

.testimg(){
    display: inherit;
}

img.test {
    .testimg;
}
爱的那么颓废 2025-01-13 19:27:39

知道了!

失败的不是分配。看来你只需要为每个可能的条件定义一个混合默认条件(如@pixshatterer指出< /a>)。

所以就我而言,我只需要这样做:

@show-image: true;

.testimg() when not (@show-image) {
    display: none;
}

.testimg() when (@show-image) {}

img.test {
    .testimg;
}

这让我需要指出关于 Less 的两件事。第一个是关于编译器消息,它们应该更具描述性;第二个是关于语言本身的,我认为不自己定义默认情况仍然太愚蠢了。

不要误会我的意思,我认为 Less 很好,而且它非常适合我当前的需求,我只是说它还很年轻,让你重新考虑将它用于大型、重要的项目。

Got it!

It wasn't the assignation what was failing. It seems you just need to define a mixin for each possible condition the default condition (as @pixshatterer pointed out).

So in my case, I just have to do this:

@show-image: true;

.testimg() when not (@show-image) {
    display: none;
}

.testimg() when (@show-image) {}

img.test {
    .testimg;
}

This gives me two things to point out about Less. First one is about the compiler messages, they should be more descriptive; the second one is about the language itself, I think it's still too dumb to not define default cases on its own.

Don't get me wrong, I think Less is good, and it's perfect for my current needs, I'm just saying it's still to young and make you reconsider using it for big, important projects.

浮生未歇 2025-01-13 19:27:39

看起来你没有正确使用它。您需要将 @show-image 传递到函数 .testimg() 中,因此结果应该类似于:

@show-image: true;

.testimg (@show) when not (@show) {
    display: none;
}

img.test {
    .testimg(@show-image);
}

我尝试使用 dotless 并且它一直在 mixin 定义的右括号上给出解析错误。看起来是时候进行错误报告了。

It looks like you are not using it quite right. You need to pass @show-image into the function .testimg(), so the result should look something like:

@show-image: true;

.testimg (@show) when not (@show) {
    display: none;
}

img.test {
    .testimg(@show-image);
}

I tried to test this using dotless and it just kept giving a parse error on the closing parenthesis of the mixin definition. It looks like it's time for a bug report there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文