How do you make a LESS mixin for gradients with a

2019-07-18 04:39发布

问题:

All of the Less mixins I have found for gradients have only a fixed number of stops. The clash between less and css in the use f the comma makes variable stops impossible to do in the same way.

Current mixin that I use for 2 way gradients

.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;
}

and for 3 way

    .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;
    }

回答1:

No Separate Variable Needed

All that you need is to make sure you use a semicolon as a separator for the parameters, even if that happens to just be only one parameter you are passing. So this works:

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 Output

.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;
}


回答2:

You have to pull your gradient styles into the mixin call using a separate variable.

 @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;
}