使用 Less 生成供应商前缀(或相关项目)并不是最好的方法。最好使用 Prefix Free 或 Auto Prefixer 等库。
话虽如此,对于您的情况,我认为为图像使用单独的参数将是最佳选择。仅当输入参数是 URL 时,该isurl()
函数才返回。true
参数的默认值@image
设置为,none
因为这不是 URL,并且会处理空白/空值处理。
.horizontal(@start-color: #555; @end-color: #333; @start-percent: 0%; @end-percent: 100%; @image: none) {
background:@start-color;
& when (isurl(@image)){
background-image: @image, -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @image, -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @image, linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
& when not (isurl(@image)){
background-image: -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
background-repeat: repeat-x;
}
.foo{
.horizontal(@image: url('bg.png'));
}
.foo2{
.horizontal(@image: 'bg.png');
}
在上面的 mixin 中,根据@image
变量的值是否是 URL,将生成适当的输出。
在某些情况下,除了渐变之外,您可能还想使用颜色(而不是/除了图像),为此您可以进一步增强 mixin,如下所示:
.horizontal(@start-color: #555; @end-color: #333; @start-percent: 0%; @end-percent: 100%; @image: none; @color: none) {
background:@start-color;
& when (isurl(@image)) and not (iscolor(@color)){
background-image: @image, -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @image, -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @image, linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
& when (iscolor(@color)) and not (isurl(@image)){
background-image: @color, -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @color, -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @color, linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
& when (isurl(@image)) and (iscolor(@color)){
background-image: @color, @image, -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @color, @image, -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: @color, @image, linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
& when not (isurl(@image)) and not (iscolor(@color)){
background-image: -webkit-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: -o-linear-gradient(left, @start-color @start-percent, @end-color @end-percent);
background-image: linear-gradient(to right, @start-color @start-percent, @end-color @end-percent);
}
background-repeat: repeat-x;
}
.foo{
.horizontal(@image: url('bg1.png'));
}
.foo2{
.horizontal(@image: 'bg.png');
}
.foo3{
.horizontal(@color: blue);
}
.foo3{
.horizontal(@color: red; @image: url('abc.gif'));
}
注意:我background-image
在上面的示例中使用了属性,但是如果我们想使用纯色以及渐变和/或图像,那么background
应该使用属性。