多个属性在 mixin 中被视为单独的参数
我正在尝试编写一个 mixin,但我似乎无法让参数按照我想要的方式工作:多个属性被视为一个单独的参数。
当前代码
.transition(@property: all, @time: 1s, @timing: ease-in-out) {
-moz-transition: @property @time @timing;
-webkit-transition: @property @time @timing;
-o-transition: @property @time @timing;
transition: @property @time @timing;
}
a {
.transition(color, opacity, .5s);
}
期望输出
a {
-moz-transition: color, opacity .5s ease-in-out;
-webkit-transition: color, opacity .5s ease-in-out;
-o-transition: color, opacity .5s ease-in-out;
transition: color, opacity .5s ease-in-out;
}
实际输出
a {
-moz-transition: color opacity .5s;
-webkit-transition: color opacity .5s;
-o-transition: color opacity .5s;
transition: color opacity .5s;
}
有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 LESS 的转义函数,即:
虽然 LESS 似乎接受这一点,但它只会对您发送的以逗号分隔的字符串中的最后一个属性进行动画处理。可惜了。
I'd suggest using LESS's escape function, i.e.:
And though it seems that LESS accepts that, it will only animate the last property in the comma-separated string you send through. A pity.
使用此处找到的解决方案适用于<强>一个和多个参数:
以这种方式使用它:
产生:
Using the solution found here works with one AND multiple arguments:
Using it this way:
yields:
如果您想将逗号分隔的列表作为参数传递给 mixin,您可以简单地使用分号来分隔参数。
下面的代码可以根据需要与您定义的 mixin 配合使用:
If you want to pass a comma-separated list as an argument to mixin, you can simply use a semicolon to separate arguments.
Code below works as desired with the mixin you defined:
Less mixin 能够使用分号分隔的参数(以及逗号分隔)。 他们建议您始终使用分号。
如果调用 mixin 时参数列表中存在分号,则分号之间的所有内容都将被视为参数,即使这些内容中包含逗号。这允许您将逗号分隔的列表作为 ONE 参数传递。如果分号不存在,它将把逗号视为参数分隔符。
输出:
请注意速记
syntax >transition
属性必须是以逗号分隔的单个转换列表。因此,b
有一个无效值,a
有两个转换,其中未指定的值默认为其初始值:color 0s escape 0s
这可能不是您想要的。 (看起来您想要
color .5s escape-in-out 0s
和opacity .5s escape-in-out 0s
。)现在您可能想知道,“如何做当没有其他参数时,我将逗号分隔的列表作为单个参数传递?”只需在列表末尾添加一个虚拟分号即可。
输出:
同样,
i
的语法无效,b
有两个转换:color 0s escape 0s
opacity 1s escape-in-输出 0
Less mixins have the ability to use semicolon-separated arguments (as well as comma-separated). They recommend you always use semicolons.
If a semicolon is present in a list of arguments when the mixin is called, everything between semicolons will be considered as arguments, even if those things have commas in them. This allows you to pass a comma-separated list as ONE argument. If a semicolon is NOT present, it will treat commas as argument separators.
output:
Note that the syntax of the shorthand
transition
property must be a comma-separated list of single transitions. Sob
has an invalid value, anda
has two transitions, in which the unspecified values default to their initial value:color 0s ease 0s
opacity .5s ease-in-out 0s
This is likely not what you intended. (It looks like you wanted
color .5s ease-in-out 0s
andopacity .5s ease-in-out 0s
.)Now you might be wondering, "how do I pass a comma-separated list as a single argument, when there are no other arguments?" Simply append a dummy semicolon at the end of the list.
output:
Again, syntax for
i
is invalid, andb
has two transitions:color 0s ease 0s
opacity 1s ease-in-out 0s