I would like to create a Sass mixin for the box-shadow property but am running into some trouble. Some of the existing code looks like this.
我想为box-shadow属性创建一个Sass mixin,但遇到了一些麻烦。一些现有代码看起来像这样。
#someDiv {
-moz-box-shadow:0 0 5px rgba(0,0,0,.25);
}
#someOtherDiv {
-moz-box-shadow:0 0 5px rgba(0,0,0,.25) inset;
}
#theLastDiv {
-moz-box-shadow: 0 0 5px rgba(0,0,0,.25), 0 1px 0 rgba(255,255,255,.2) inset;
}
Trying to roll all of this into 1 mixin is becoming problematic. The documentation for using logic within mixins is pretty sparse.
试图将所有这些融入1 mixin正成为问题。在mixins中使用逻辑的文档非常稀疏。
I would like to create some mixin along the lines of:
我想创建一些mixin:
@mixin boxShadow($xOffSet, $yOffSet, $blur, $red, $green, $blue, $opacity, $inset : false) {
@if $inset == true {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue) inset;
} @else {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue);
}
}
This is throwing errors because I guess Sass can't evaluate the $inset variable.
这是抛出错误,因为我猜Sass无法评估$ inset变量。
The previous example only demonstrates the problem I am having when it comes to to box-shadow being inset or not. The other problem I am having is when there are multiple box-shadows being declared on a single element. Refer to #theLastDiv described above if for reference.
前面的例子只展示了我遇到的盒子阴影的问题。我遇到的另一个问题是,在单个元素上声明了多个框阴影。如果需要参考,请参阅上述#theLastDiv。
@mixin boxShadow($declarations : 2, $xOffSet1, $yOffSet1, $blur1, $red1, $green1, $blue1, $opacity1 $xOffSet2, $yOffSet2, $blur2, $red2, $green2, $blue2, $opacity2) {
@if $declaratiOns== 1 {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue);
} @else if $declaratiOns== 2 {
-moz-box-shadow: #{$xOffSet1}px #{$yOffSet1}px #{$blur1}px rgba($red1,$green1,$blue1), #{$xOffSet2}px #{$yOffSet2}px #{$blur2}px rgba($red2,$green2,$blue2);
}
Sometimes an element has one box-shadow and other times it has to separate box-shadows. Am I mistaken that Sass lacks the ability to decipher this logic and that to accomplish this would require separate Mixins (one for elements with one box shadow, another for mixins with two box shadows).
有时一个元素有一个盒子阴影,有时它必须分隔盒子阴影。我错误地认为Sass缺乏破译这种逻辑的能力而且要实现这一目标需要单独的Mixins(一个用于带有一个盒子阴影的元素,另一个用于具有两个盒子阴影的mixins)。
And to complicate the matter, how does the opacity problem described above factor into this? Would love to get some feedback on this. Let me know if I'm mistaken or the way I'm thinking about the problem is flawed in general. Thanks!
并且为了使问题复杂化,上面描述的不透明度问题如何影响到这个?很想得到一些反馈。让我知道,如果我弄错了,或者我正在考虑这个问题的方式总体上是有缺陷的。谢谢!
19
You can use a variable argument like this:
你可以使用这样的变量参数:
// Box shadows
@mixin box-shadow($shadow...) {
-webkit-box-shadow: $shadow;
-moz-box-shadow: $shadow;
box-shadow: $shadow;
}
That allows you to have commas in the argument passed. so you can pass all the shadows you want.
这允许您在传递的参数中使用逗号。所以你可以传递你想要的所有阴影。
An example using it:
使用它的一个例子:
@include box-shadow(2px 2px 2px rgba(#ccc, .8), -2px -2px 2px rgba(#ccc, 0.8)) ;
Pass in color variables like this:
传入这样的颜色变量:
$shadow-color: red; // could also be a #F9F8F6 type color
@include box-shadow(0 2px 2px rgba($shadow-color, .9));
UPDATE
UPDATE
$shadow-color: red; // could also be a hex (like #F9F8F6) type color
@include box-shadow(0 2px 2px rgba($shadow-color, .9));
In case you haven't seen arguments with the elipsis allowing a variable number of arguments before (ie splats) check this link: http://sass-lang.com/documentation/file.SASS_CHANGELOG.html#variable_keyword_arguments
如果您没有看到elipsis的参数允许之前的可变数量的参数(即splats),请检查此链接:http://sass-lang.com/documentation/file.SASS_CHANGELOG.html#variable_keyword_arguments
18
I prefer to keep basic CSS parameter without commas: 0 0 1px rgba(0, 0, 0, .5)
rather than with commas: 0, 0, 5, 0, 0, 0, .25
.
我更喜欢保留没有逗号的基本CSS参数:0 0 1px rgba(0,0,0,.5)而不是逗号:0,0,5,0,0,0,.25。
This is my solution:
这是我的解决方案:
@mixin box-shadow($shadow1, $shadow2:false, $shadow3:false, $shadow4:false, $shadow5:false) {
$params: $shadow1;
@if $shadow2
{ $params: $shadow1, $shadow2; }
@if $shadow3 != false
{ $params: $shadow1, $shadow2, $shadow3; }
@if $shadow4 != false
{ $params: $shadow1, $shadow2, $shadow3, $shadow4; }
@if $shadow5 != false
{ $params: $shadow1, $shadow2, $shadow3, $shadow4, $shadow5; }
-webkit-box-shadow: $params;
-moz-box-shadow: $params;
box-shadow: $params;
}
@include box-shadow(-1px -1px 2px rgba(0, 0, 0, .05), 0 1px 1px white inset), ...
5
You could use collections and only have one parameter:
您可以使用集合,只有一个参数:
@mixin box-shadow($params) {
-webkit-box-shadow: $params;
-moz-box-shadow: $params;
box-shadow: $params;
}
$shadows: 0px 0px 5px rgba(0, 0, 0, 0.25), 0px 1px 0px #000 inset;
.myclass {
@include box-shadow($shadows);
}
4
I've added a bit of logic to handle the cases you described. Here is the mixin:
我添加了一些逻辑来处理你描述的情况。这是mixin:
@mixin boxShadow($xOffSet, $yOffSet, $blur, $red, $green, $blue, $opacity, $inset : false, $two : false, $xOffSet2 : 0, $yOffSet2 : 0, $blur2 : 0, $red2 : 0, $green2 : 0, $blue2 : 0, $opacity2 : 0, $inset2 : false) {
@if $inset {
@if $two {
@if $inset2 {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity) inset, #{$xOffSet2}px #{$yOffSet2}px #{$blur2}px rgba($red2,$green2,$blue2, $opacity2) inset;
} @else {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity) inset, #{$xOffSet2}px #{$yOffSet2}px #{$blur2}px rgba($red2,$green2,$blue2, $opacity2);
}
} @else {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity) inset;
}
} @else {
@if $two {
@if $inset2 {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity), #{$xOffSet2}px #{$yOffSet2}px #{$blur2}px rgba($red2,$green2,$blue2, $opacity2) inset;
} @else {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity), #{$xOffSet2}px #{$yOffSet2}px #{$blur2}px rgba($red2,$green2,$blue2, $opacity2);
}
} @else {
-moz-box-shadow: #{$xOffSet}px #{$yOffSet}px #{$blur}px rgba($red,$green,$blue, $opacity);
}
}
}
The mixin takes 17 parameters. Sorry for having so many, but SASS does not handle arrays or objects. In any event, 10 are optional. They are:
mixin需要17个参数。很抱歉有这么多,但SASS不处理数组或对象。无论如何,10是可选的。他们是:
You could set your styles like this:
您可以像这样设置样式:
#someDiv {
@include boxShadow(0, 0, 5, 0, 0, 0, .25);
}
#someOtherDiv {
@include boxShadow(0, 0, 5, 0, 0, 0, .25, true);
}
#theLastDiv {
@include boxShadow(0, 0, 5, 0, 0, 0, .25, false, true, 0, 1, 0, 255, 255, 255, .2, true);
}
Which generates the following CSS:
哪个生成以下CSS:
/* line 9, ../src/screen.scss */
#someDiv {
-moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.25);
}
/* line 12, ../src/screen.scss */
#someOtherDiv {
-moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.25) inset;
}
/* line 16, ../src/screen.scss */
#theLastDiv {
-moz-box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.25), 0px 1px 0px rgba(255, 255, 255, 0.2) inset;
}
0
Compass code might be worth studying:
指南针代码可能值得研究:
However, they seem to use some server side helpers as well.
但是,他们似乎也使用了一些服务器端助手。
@mixin box-shadow(
$shadow-1 : default,
$shadow-2 : false,
$shadow-3 : false,
$shadow-4 : false,
$shadow-5 : false,
$shadow-6 : false,
$shadow-7 : false,
$shadow-8 : false,
$shadow-9 : false,
$shadow-10: false
) {
@if $shadow-1 == default {
$shadow-1 : -compass-space-list(compact(if($default-box-shadow-inset, inset, false), $default-box-shadow-h-offset, $default-box-shadow-v-offset, $default-box-shadow-blur, $default-box-shadow-spread, $default-box-shadow-color));
}
$shadow : compact($shadow-1, $shadow-2, $shadow-3, $shadow-4, $shadow-5, $shadow-6, $shadow-7, $shadow-8, $shadow-9, $shadow-10);
@include experimental(box-shadow, $shadow,
-moz, -webkit, not -o, not -ms, not -khtml, official
);
}