mixin 参数的默认值

发布于 2025-01-06 13:44:53 字数 589 浏览 6 评论 0原文

我正在使用像这样的字体混合:

#font {
  .trebuchet(@weight: normal, @size: 12px, @lineHeight: 20px, @style:normal) {
    font-family: "Trebuchet MS", arial, verdana, sans-serif;
    font-size: @size;
    font-weight: @weight;
    line-height: @lineHeight;
    font-style: @style;
  }  
}

我想调用它仅更改最后一个参数样式,但将其他值保留为默认值。例如,我不会写:

#font > .trebuchet(normal, 12px, 20px, italic);

我会写类似的内容:

#font > .trebuchet(false, false, false, italic);

(这实际上有效,但我怀疑出于错误的原因 - 或者无论如何它看起来不正确的语法) 实现这一目标的最佳方法是什么?

I am using a mixin for font like this:

#font {
  .trebuchet(@weight: normal, @size: 12px, @lineHeight: 20px, @style:normal) {
    font-family: "Trebuchet MS", arial, verdana, sans-serif;
    font-size: @size;
    font-weight: @weight;
    line-height: @lineHeight;
    font-style: @style;
  }  
}

I want to call this changing only the last parameter, style, but leave the other values to their defaults. For example, instead of writing:

#font > .trebuchet(normal, 12px, 20px, italic);

I would write something like:

#font > .trebuchet(false, false, false, italic);

(which actually works, but I suspect for the wrong reasons -or anyway it doesn't seem proper syntax)
What's the best way to achieve this?

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

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

发布评论

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

评论(1

星軌x 2025-01-13 13:44:53

您可以使用模式匹配。你应该像这样克隆你的 .trebuchet 类:

.trebuchet when (@weight=false) and (not(ispixel(@size)) and (not(ispixel(@lineHeight)) {
    font-style: @style;
}

You can use pattern matching. You should clone your .trebuchet class like this:

.trebuchet when (@weight=false) and (not(ispixel(@size)) and (not(ispixel(@lineHeight)) {
    font-style: @style;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文