![](/img/trans.png)
[英]CSS Less watch compiler - how to pass parameter and generate multiple copies of the CSS based on parameters?
[英]How to Pass Parameters with Commas in Less
我有一个mixin,它带有形状的名称及其坐标。 我想知道如果坐标包含逗号,如何传递坐标?
.clip-path(@shape @coords) {
-webkit-clip-path: @shape(@coords);
-moz-clip-path: @shape(@coords);
clip-path: @shape(@coords);
}
.foo {
.clip-path(polygon, 0 0, 0 100%, 100% 0);
/*
I am trying to achieve:
clip-path: polygon(0 0, 0 100%, 100% 0);
*/
}
注意:我赞同torazaburo的所有评论。 请不要使用Less mixins作为添加前缀的方法。 使用诸如AutoPrefixer或Free Prefix-free之类的前缀工具要简单得多。
就是说,以下是实现所需输出的几种方法:
.clip-path(@shape, @coords) {
-webkit-clip-path: ~"@{shape}(@{coords})";
-moz-clip-path: ~"@{shape}(@{coords})";
clip-path: ~"@{shape}(@{coords})"; /* use interpolation to display the output */
}
.foo {
.clip-path(polygon, ~"0 0, 0 100%, 100% 0"); /* pass the values as one single string */
}
或者,使用如下所示的高级@rest
变量选项 。 这是一种将可变数量的args传递给mixin并仍然使其与mixin定义相匹配的方法。
.clip-path(@shape; @coords...) {
-webkit-clip-path: ~"@{shape}(@{coords})";
-moz-clip-path: ~"@{shape}(@{coords})";
clip-path: ~"@{shape}(@{coords})";
}
.foo {
.clip-path(polygon; 0 0, 0 100%, 100% 0);
}
.foo2 {
.clip-path(polygon; 0 0, 0 100%, 100% 0, 100% 100%); /* Less compiler will pick all values after the first as coords */
}
或者,如果mixin仅用于添加供应商前缀(如前所述,我不建议这样做),则最简单的选择如下:
.clip-path(@args) {
-webkit-clip-path: @args;
-moz-clip-path: @args;
clip-path: @args;
}
.foo {
.clip-path(~"polygon(0 0, 0 100%, 100% 0)"); /* just pass the whole thing as a string */
}
解决方法之一是使用临时变量:
.foo {
@workAround: 0 0, 0 100%, 100% 0;
.clip-path(polygon, @workAround);
}
当您将变量传递到mixin时,也可以转义该值:
.foo {
.clip-path(polygon, ~"0 0, 0 100%, 100% 0");
}
这两个都确保传递给mixin的值是一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.