gpt4 book ai didi

css - 如何在 Less 中传递带逗号的参数

转载 作者:行者123 更新时间:2023-11-28 11:05:44 25 4
gpt4 key购买 nike

我有一个 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);
*/
}

最佳答案

Note: I second all comments made by torazaburo. Please don't use Less mixins as a way to add prefixes. It is much more simpler to use a prefixing tool like AutoPrefixer or 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 */
}

或者,使用 advanced @rest variable option像下面。这是一种将可变数量的 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 */
}

关于css - 如何在 Less 中传递带逗号的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35836099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com