使用可选参数制作 Sass mixin
我正在编写这样的 mixin:
@mixin box-shadow($top, $left, $blur, $color, $inset:"") {
-webkit-box-shadow: $top $left $blur $color $inset;
-moz-box-shadow: $top $left $blur $color $inset;
box-shadow: $top $left $blur $color $inset;
}
当调用时,我真正想要的是,如果没有传递 $inset
值,则不会输出任何内容,而不是编译成这样的内容:
-webkit-box-shadow: 2px 2px 5px #555555 "";
-moz-box-shadow: 2px 2px 5px #555555 "";
box-shadow: 2px 2px 5px #555555 "";
How do I rewrite the mixin这样,如果没有传递 $inset
值,则不会输出任何内容?
I am writing a mixin like this:
@mixin box-shadow($top, $left, $blur, $color, $inset:"") {
-webkit-box-shadow: $top $left $blur $color $inset;
-moz-box-shadow: $top $left $blur $color $inset;
box-shadow: $top $left $blur $color $inset;
}
When called what I really want is that if no $inset
value is passed, nothing is output, rather than it compiling to something like this:
-webkit-box-shadow: 2px 2px 5px #555555 "";
-moz-box-shadow: 2px 2px 5px #555555 "";
box-shadow: 2px 2px 5px #555555 "";
How do I rewrite the mixin so that if there is no value of $inset
passed, nothing is output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
干巴巴的做法
而且,一般来说,这是一个删除引号的巧妙技巧。
SASS 版本 3+,您可以使用
unquote()
:选择在这里:使用 SASS 将列表作为单个参数传递给 mixin
A DRY'r Way of Doing It
And, generally, a neat trick to remove the quotes.
SASS Version 3+, you can use
unquote()
:Picked this up over here: pass a list to a mixin as a single argument with SASS
更好的 DRY 方式
是将
$args...
传递给@mixin
。这样,无论您将传递多少个
$args
。在
@input
调用中,您可以传递所需的所有参数。像这样:
现在你可以通过传递所有需要的参数来在你想要的每个类中重用你的盒子阴影:
A so much better DRY way
is to pass
$args...
to the@mixin
.That way, no matter how many
$args
you will pass.In the
@input
call, you can pass all args needed.Like so:
And now you can reuse your box-shadow in every class you want by passing all needed args:
Sass 支持
@if
语句。 (请参阅文档。)您可以像这样编写您的 mixin :
Sass supports
@if
statements. (See the documentation.)You could write your mixin like this:
您可以将带有 null 的属性作为默认值,如果您不传递参数,则不会对其进行解释。
这意味着您可以像这样编写 include 语句。
而不是这样写。
如此处的答案所示
You can put the property with null as a default value and if you don't pass the parameter it will not be interpreted.
This means you can write the include statement like this.
Instead of writing it like this.
As shown in the answer here
老问题,我知道,但我认为这仍然相关。可以说,一种更清晰的方法是使用 unquote() 函数(SASS 从 3.0.0 版本开始就有这个函数):
这大致相当于 Josh 的答案,但我认为显式命名的函数比字符串插值更容易混淆句法。
Old question, I know, but I think this is still relevant. Arguably, a clearer way of doing this is to use the unquote() function (which SASS has had since version 3.0.0):
This is roughly equivalent to Josh's answer, but I think the explicitly named function is less obfuscated than the string interpolation syntax.
我知道这并不完全是您正在寻找的答案,但是当
@include box-shadow
mixin 时,您可以将"null"
作为最后一个参数传递,就像这样@include box-shadow(12px, 14px, 2px, green, null);
现在,如果该参数在该属性中只有一个,则该属性(及其(默认)值)将不会被编译。如果该“行”上有两个或多个参数,则只有您清空的参数不会被编译(您的情况)。CSS 输出正是您想要的,但您必须编写
null
:)I know its not exactly the answer you were searching for but you could pass
"null"
as last argument when@include box-shadow
mixin, like this@include box-shadow(12px, 14px, 2px, green, null);
Now, if that argument is only one in that property than that property (and its (default) value) won't get compiled. If there are two or more args on that "line" only ones that you nulled won't get compiled (your case).CSS output is exactly as you wanted it, but you have to write your
null
s :)这是我使用的解决方案,注释如下:
here is the solution i use, with notes below:
更干燥的方式!
现在您可以更智能地重复使用您的盒子阴影:
Even DRYer way!
And now you can reuse your box-shadow even smarter:
使用 [email protected] :
并且在没有值的情况下使用,按钮将呈蓝色
并且有了值,按钮就会变成红色
,也适用于六角形
With [email protected] :
and use without value, button will be blue
and with value, button will be red
works with hexa too
超级简单的方法
只需将默认值
none
添加到 $inset - 所以现在当没有传递 $inset 时,将不会显示任何内容。
Super simple way
Just add a default value of
none
to $inset - soNow when no $inset is passed nothing will be displayed.
您始终可以将 null 分配给可选参数。这是一个简单的修复
You can always assign null to your optional arguments. Here is a simple fix
我是 css 编译器的新手,希望这有帮助,
I am new to css compilers, hope this helps,