sass @mixin 可以接受未定义数量的参数吗?
我正在尝试创建一个用于过渡的 sass mixin。这是我到目前为止所拥有的。
@mixin transition($var)
-webkit-transition: $var
transition: $var
我希望能够像这样传递多个参数
@include transition(color .5s linear, width .5s linear)
不幸的是,我收到以下错误
Syntax error: Mixin transition takes 1 argument but 2 were passed.
有没有办法做到这一点,以便它在 css 中生成以下输出,同时仍然接受未定义数量的参数?
-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;
I'm trying to create a sass mixin for transitions. This is what I have so far.
@mixin transition($var)
-webkit-transition: $var
transition: $var
I want to be able to pass it multiple arguments like this
@include transition(color .5s linear, width .5s linear)
Unfortunately, I get the following error
Syntax error: Mixin transition takes 1 argument but 2 were passed.
Is there a way to do this so it produces the following output in css while still accepting an undefined number of arguments?
-webkit-transition: color .5s linear, width .5s linear;
transition: color .5s linear, width .5s linear;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
变量参数
有时,mixin 接受未知数量的参数是有意义的。例如,用于创建盒子阴影的 mixin 可能采用任意数量的阴影作为参数。对于这些情况,Sass 支持“变量参数”,它们是 mixin 声明末尾的参数,它获取所有剩余参数并将它们打包为列表。这些参数看起来就像普通参数,但后面跟着
...
。例如:编译为:
From : SASS 官方文档
所以你基本上只需要更改你的mixins 声明如下所示:
Variable Arguments
Sometimes it makes sense for a mixin to take an unknown number of arguments. For example, a mixin for creating box shadows might take any number of shadows as arguments. For these situations, Sass supports “variable arguments,” which are arguments at the end of a mixin declaration that take all leftover arguments and package them up as a list. These arguments look just like normal arguments, but are followed by
...
. For example:is compiled to:
From : SASS official Documentation
So you basically just need to change your mixins declaration to look like this :
当您调用 mixin 时,请像这样调用它:
带有额外的括号。这将使 sass 明白您希望将其用作单个参数。
编辑:如果使用 Sass 3.2 或更高版本,请参阅上面 Jeremie Parker 的回答。 3.2 中添加了实变量参数: http: //chriseppstein.github.io/blog/2012/08/23/sass-3-2-is-released
When you call the mixin, call it like this:
With the extra parens. This will key sass into the fact that you want this used as a single argument.
EDIT: See Jeremie Parker's answer above if using Sass 3.2 or later. Real Variable Arguments were added in 3.2: http://chriseppstein.github.io/blog/2012/08/23/sass-3-2-is-released
如果你想要多个参数和供应商前缀,就像下面的场景:
预期
我建议你这个Mixin ,我发现 无意义写作。
代码
In case you want multiple arguments and vendor-prefixed, like the fallowing scenario:
Expected
I suggest you this Mixin, I found on Meaningless Writing.
Code
Compass 有一个过渡 mixin,你可以看一下(或者你可以只使用 Compass)。您可以在这里更好地查看它: http://beta.compass- style.org/reference/compass/css3/transition/。
从表面上看,你不能执行未定义数量的 mixin,因为 Compass 的维护者也帮助维护 Sass,你可以看到他为他的转换 mixin 定义了最多 10 个单独的参数。
Compass has a transition mixin that you could take a look at (or you could just use Compass). You can take a better look at it here: http://beta.compass-style.org/reference/compass/css3/transition/.
By the looks of it you can't do an undefined number of mixins as the maintainer of Compass also aids in maintaining Sass and you can see that he has defined a maximum number of 10 separate arguments for his transition mixin.