如何为具有可变停止次数的渐变制作一个LESS mixin?

时间:2013-12-20 16:35:50

标签: css less gradients

我发现渐变的所有Less mixins只有一定数量的停顿。逗号中使用less和css之间的冲突使得变量停止不可能以相同的方式进行。

我用于双向渐变的当前mixin

.gradient (@origin: left, @step-1: @white,  @step-2: @black, @fallback: @step-1){
    background-color: @fallback;
    background: -webkit-linear-gradient(@origin, @step-1, @step-2) @fallback no-repeat;
    background:    -moz-linear-gradient(@origin, @step-1, @step-2) @fallback no-repeat;
    background:    -ms-linear-gradient(@origin, @step-1, @step-2) @fallback no-repeat;
    background:      -o-linear-gradient(@origin, @step-1, @step-2) @fallback no-repeat;
    background:         linear-gradient(@origin, @step-1, @step-2) @fallback no-repeat;
}

和3路

    .gradient-3-way (@origin: left,  @step-1: @white,  @step-2: @black,  @step-3: @white, @fallback: @step-1){
        background-color: @fallback;
        background: -webkit-linear-gradient(@origin, @step-1, @step-2, @step-3) @fallback no-repeat;
        background:    -moz-linear-gradient(@origin, @step-1, @step-2, @step-3) @fallback no-repeat;
        background:      -ms-linear-gradient(@origin, @step-1, @step-2, @step-3) @fallback no-repeat;
        background:      -o-linear-gradient(@origin, @step-1, @step-2, @step-3) @fallback no-repeat;
        background:         linear-gradient(@origin, @step-1, @step-2, @step-3) @fallback no-repeat;
    }

2 个答案:

答案 0 :(得分:4)

无需单独的变量

您需要的只是确保使用分号作为参数的分隔符,即使这恰好只是您传递的一个参数。所以这有效:

<强> LESS

@white: #fff;
.gradient (@origin: left, @fallback: @white,  @stops){
    background-color: @fallback;
    background: -webkit-linear-gradient(@origin, @stops) @fallback no-repeat;
    background:    -moz-linear-gradient(@origin, @stops) @fallback no-repeat;
    background:    -ms-linear-gradient(@origin, @stops) @fallback no-repeat;
    background:      -o-linear-gradient(@origin, @stops) @fallback no-repeat;
    background:         linear-gradient(@origin, @stops) @fallback no-repeat;
}

.test {
   .gradient(@stops: #fff 0, #000 20px, #000 20px, #f00 20px;)
}                                                           |
                                                    this final semicolon 
                                                    causes the commas to 
                                                    become list separators
                                                    instead of parameter
                                                    separators making the whole 
                                                    thing part of one variable

CSS输出

.test {
  background-color: #ffffff;
  background: -webkit-linear-gradient(left, #ffffff 0, #000000 20px, #000000 20px, #ff0000 20px) #ffffff no-repeat;
  background: -moz-linear-gradient(left, #ffffff 0, #000000 20px, #000000 20px, #ff0000 20px) #ffffff no-repeat;
  background: -ms-linear-gradient(left, #ffffff 0, #000000 20px, #000000 20px, #ff0000 20px) #ffffff no-repeat;
  background: -o-linear-gradient(left, #ffffff 0, #000000 20px, #000000 20px, #ff0000 20px) #ffffff no-repeat;
  background: linear-gradient(left, #ffffff 0, #000000 20px, #000000 20px, #ff0000 20px) #ffffff no-repeat;
}

答案 1 :(得分:1)

您必须使用单独的变量将渐变样式拉入mixin调用。

 @grady: right, #fff 0, #000 20px, #000 20px, #f00 20px;
.red{.gradient-multi (@grady);}
.gradient-multi (@grad: right){
    background: -webkit-linear-gradient(@grad) no-repeat;
    background:    -moz-linear-gradient(@grad) no-repeat;
    background:    -ms-linear-gradient(@grad) no-repeat;
    background:      -o-linear-gradient(@grad) no-repeat;
    background:         linear-gradient(@grad) no-repeat;
}