可以通过 lesscss 中的另一个变量设置参数默认值吗?

发布于 2024-11-23 23:30:04 字数 1268 浏览 1 评论 0原文

使用 lesscss 时是否可以通过另一个变量声明默认参数?我已经尝试过但无法让它工作。实际上可能吗?

这里的目的是将@button_bg_color设置为默认值,而不声明@button_gradient_from和@button_gradient_to

@button_bg_color: #9cc961;
@button_txt_color: #fff;

@darkened_button_bg_color: @button_bg_color - #2b2b2b;

.branded_button(@from_color: @button_bg_color, @to_color: @darkened_button_bg_color) {
  color: @button_txt_color;
  background-color: @from_color;
  /* FireFox 3.6 */
  background-image: -moz-linear-gradient(top, @from_color, @to_color);
  /* Safari4+, Chrome */
  background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, @from_color),color-stop(1, @to_color));
}

.button {
  a {
    .branded_button(@button_gradient_from, @button_gradient_to);
  }
}

++++++++++++++++++++++++++++++++++++++++++++++

编辑:更改为混合类似的帖子:are variable mixin name in LESS possible? 但这失败了具有以下渲染:

.branded_button(@from_color: @button_bg_color {
  color:#ffffff;
  background-color:;
  background-image:-moz-linear-gradient(top,,);
  background-image:-webkit-gradient(linear,left bottom,left top,color-stop(0,),color-stop(1,));
}

When using lesscss is it possible to declare a default parameter via another variable? I've tried it but can't get it to work. Is it actually possible?

The intent here is to set @button_bg_color as the default should @button_gradient_from and @button_gradient_to not be declared.

@button_bg_color: #9cc961;
@button_txt_color: #fff;

@darkened_button_bg_color: @button_bg_color - #2b2b2b;

.branded_button(@from_color: @button_bg_color, @to_color: @darkened_button_bg_color) {
  color: @button_txt_color;
  background-color: @from_color;
  /* FireFox 3.6 */
  background-image: -moz-linear-gradient(top, @from_color, @to_color);
  /* Safari4+, Chrome */
  background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, @from_color),color-stop(1, @to_color));
}

.button {
  a {
    .branded_button(@button_gradient_from, @button_gradient_to);
  }
}

++++++++++++++++++++++++++++++++++++++++++

Edit: changed to a mixing per this similar post: are variable mixin names in LESS possible? but this fails with the following rendering:

.branded_button(@from_color: @button_bg_color {
  color:#ffffff;
  background-color:;
  background-image:-moz-linear-gradient(top,,);
  background-image:-webkit-gradient(linear,left bottom,left top,color-stop(0,),color-stop(1,));
}

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

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

发布评论

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

评论(1

寒江雪… 2024-11-30 23:30:05

您可以在局部范围内重新定义全局变量。 Mixin 将使用新值进行调用。在局部范围之外,全局变量将保留其值。请参阅此 LESS 代码。

/* global variables */
@f-border-width: 4px;
@f-border-style: solid;
@f-border-color: #BE2;

/* default parameters - global variables */
.f-border (
        @width: @f-border-width, 
        @style: @f-border-style,
        @color: @f-border-color
    ) {
    border: @width @style @color;
}

/* using initial value of global variable "@f-border-color" */
.base-1 {
    .f-border;
}

/* changing global variable "@f-border-color" in the local scope */
.extension {
    @f-border-color: #F16;
    .f-border;
}

/* outside of the local scope of ".extension" */
/* global variable "@f-border-color" preserves its value */
.base-2 {
    .f-border;
}

它将被编译到此 CSS 代码中。

.base-1 {
  border: 4px solid #bbee22;
}

.extension {
  border: 4px solid #ff1166;
}

.base-2 {
  border: 4px solid #bbee22;
}

请参阅演示

You can redefine a global variable in the local scope. Mixin will be invoked with a new value. Outside the local scope the global variable will preserve its value. See this LESS code.

/* global variables */
@f-border-width: 4px;
@f-border-style: solid;
@f-border-color: #BE2;

/* default parameters - global variables */
.f-border (
        @width: @f-border-width, 
        @style: @f-border-style,
        @color: @f-border-color
    ) {
    border: @width @style @color;
}

/* using initial value of global variable "@f-border-color" */
.base-1 {
    .f-border;
}

/* changing global variable "@f-border-color" in the local scope */
.extension {
    @f-border-color: #F16;
    .f-border;
}

/* outside of the local scope of ".extension" */
/* global variable "@f-border-color" preserves its value */
.base-2 {
    .f-border;
}

It will be compiled into this CSS code.

.base-1 {
  border: 4px solid #bbee22;
}

.extension {
  border: 4px solid #ff1166;
}

.base-2 {
  border: 4px solid #bbee22;
}

Please, see the demo.

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