可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i have the following mixin
@mixin arrows($arrowdirection:"left", $arrowsize:"5px", $arrowcolor:$navbgblue ) {
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
@if $arrowdirection == "right" {
border-top: $arrowsize + px;
border-bottom: $arrowsize + px;
border-left: $arrowsize + px $arrowcolor;
} @else if $arrowdirection == "left" {
border-top: $arrowsize + px;
border-bottom: $arrowsize + px;
border-right:$arrowsize + px $arrowcolor;
} @else if $arrowdirection == "up" {
border-bottom: $arrowsize + px $arrowcolor;
border-left: $arrowsize + px;
border-right: $arrowsize + px;
} @else if $arrowdirection == "down" {
border-left: $arrowsize + px;
border-right: $arrowsize + px;
border-top: $arrowsize + px $arrowcolor;
}
}
which for some (no doubt obvious) reason refuses to work
if i use either the default or pass something in
.test{
@include arrows("left", "5px", "blue");
}
i only get the CSS
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
so my if / else is somehow not kicking in.
why please ?
回答1:
yeah there is definately a bug of some sort, with the optimization.
But this works
@mixin leftarrow($size:5px, $direction:left) {
border-width: $size;
border-color: transparent;
border-style: solid;
display: inline-block;
height: 0px;
width: 0px;
@if $direction == "right" {
border-left-color: $navbgblue;
border-right-width: 0px;
} @else if $direction == "left" {
border-right-color: $navbgblue;
border-left-width: 0px;
} @else if $direction == "up" {
border-bottom-color: $navbgblue;
border-top-width: 0px;
} @else if $direction == "down" {
border-top-color: $navbgblue;
border-bottom-width: 0px;
}
}
.test{
@include leftarrow(5px, up);
}
so i'll use that :-)
回答2:
here is working mixin with just 4 lines of code:
/*
* Creates CSS triangle
* direction options: top, right, bottom, left.
* Example @include cssTriangle(bottom, red, 50px);
*/
@mixin cssTriangle($direction:left, $color:#89D4E7, $width:34px) {
$opposite: nth((top,right,bottom,left), index((bottom,left,top,right),$direction));
border: solid $width transparent;
border-#{$direction}: none;
border-#{$opposite}: solid $width $color;
}
回答3:
First off, you don't want to quote your variables unless you want them to be treated as strings (strings get quoted in your CSS output). It's a really good idea to have your default value be as a part of an "else" instead of an "else if".
Are you looking at the generated CSS or looking at it from within something like Firebug? Because if I copy/paste your mixin as is, I get this output:
.test {
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-top: "5pxpx";
border-bottom: "5pxpx";
border-right: "5pxpx" blue;
}
Here's a refactored version of your mixin with all the quotes and the extra "px" removed:
@mixin arrows($arrowdirection: left, $arrowsize: 5px, $arrowcolor: green) {
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
@if $arrowdirection == right {
border-top: $arrowsize;
border-bottom: $arrowsize;
border-left: $arrowsize $arrowcolor;
} @else if $arrowdirection == up {
border-bottom: $arrowsize $arrowcolor;
border-left: $arrowsize;
border-right: $arrowsize;
} @else if $arrowdirection == down {
border-left: $arrowsize;
border-right: $arrowsize;
border-top: $arrowsize $arrowcolor;
} @else {
border-top: $arrowsize;
border-bottom: $arrowsize;
border-right:$arrowsize $arrowcolor;
}
}
.test {
@include arrows;
}
.test2 {
@include arrows(right, 10px, blue);
}
I get the following output:
.test {
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-top: 5px;
border-bottom: 5px;
border-right: 5px green;
}
.test2 {
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-top: 10px;
border-bottom: 10px;
border-left: 10px blue;
}
回答4:
This worked for me. Using border shortcuts create mixed and mashed css that conflicted with itself
@mixin arrows($arrowdirection: left, $arrowsize: 5px, $arrowcolor: green) {
/*
* Creates css arrows
* direction options: top, right, bottom, left. (to match border-'direction' of css)
* Example @include arrows(bottom, 12px, #f00);
*/
width: 0;
height: 0;
@if $arrowdirection == top {
border-left: $arrowsize solid transparent;
border-right: $arrowsize solid transparent;
border-bottom: $arrowsize solid $arrowcolor;
} @else if $arrowdirection == right {
border-top: $arrowsize solid transparent;
border-bottom: $arrowsize solid transparent;
border-left: $arrowsize solid $arrowcolor;
} @else if $arrowdirection == bottom {
border-left: $arrowsize solid transparent;
border-right: $arrowsize solid transparent;
border-top: $arrowsize solid $arrowcolor;
} @else {
border-top: $arrowsize solid transparent;
border-bottom: $arrowsize solid transparent;
border-right: $arrowsize solid $arrowcolor;
}
}
- standing on the shoulders of http://css-tricks.com/snippets/css/css-triangle/ and @cinnamon
回答5:
You need to pass matching $arrowdirection argument while calling the @mixin.But you can add @error(your message) in seperate @else condition like this:-
$navbgblue: #587cd7;
@mixin leftarrow($size:5px, $direction:left) {
border-width: $size;
border-color: transparent;
border-style: solid;
display: inline-block;
height: 0px;
width: 0px;
@if $direction == "right" {
border-left-color: $navbgblue;
border-right-width: 0px;
} @else if $direction == "left" {
border-right-color: $navbgblue;
border-left-width: 0px;
} @else if $direction == "up" {
border-bottom-color: $navbgblue;
border-top-width: 0px;
} @else if $direction == "down" {
border-top-color: $navbgblue;
border-bottom-width: 0px;
} @else{
@error('please provide correct direction [up,right,bottom,left]')
}
}
.test{
@include leftarrow(5px, blue);
}
which forces user to provide default or matching argument and user always get complied mixin.