我目前正在尝试使用此处所述的SASS mixin的精简版本,这有助于实现线性渐变:https://www.sitepoint.com/building-linear-gradient-mixin-sass/
我的瘦身版本:
// @param {Keyword | Angle} $direction - Linear gradient direction
// @param {Arglist} $color-stops - List of color-stops composing the gradient
@mixin linear-gradient($direction, $color-stops...) {
// Direction has been omitted and happens to be a color-stop
@if is-direction($direction) == false {
$color-stops: $direction, $color-stops;
$direction: 180deg;
}
background: nth(nth($color-stops, 1), 1);
background: linear-gradient($direction, $color-stops);
}
// Test if `$value` is a valid direction
// @param {*} $value - Value to test
// @return {Bool}
@function is-direction($value) {
$is-keyword: index((to top, to top right, to right top, to right, to bottom right, to right bottom, to bottom, to bottom left, to left bottom, to left, to left top, to top left), $value);
$is-angle: type-of($value) == 'number' and index('deg' 'grad' 'turn' 'rad', unit($value));
@return $is-keyword or $is-angle;
}
当我使用它时,就像这样:
@include linear-gradient(#ededed 54%, #d9d9d9 55%);
我收到语法错误:
预计有颜色。得到:#ededed 54%
我认为问题在于这一行:
$color-stops: $direction, $color-stops;
因为我注意到当我以这种方式使用它时它工作正常:
@include linear-gradient(to top, #fff 50%, #f0f0f0 51%);
我相信我已经成功解决了类型问题,但似乎无法弄清楚如何修复它。
答案 0 :(得分:0)
我相信我找到了解决方案。
首先,我通过添加:
快速测试了$ color-stops的当前输出 &:after {
@each $color in $color-stops {
content: type_of($color);
}
}
这证实了我的担忧,因为它输出了两种不同类型,在它运行此代码的场景中:
$color-stops: $color-stops, $direction;
输出:
内容:arglist; 内容:列表;
最终,我发现解决方法是改变我附加$ direction变量的方式。
更改:
$color-stops: $direction, $color-stops;
要:
$color-stops: append($color-stops, $direction);
现在我的测试代码输出:
内容:列表; 内容:列表;
不再出错。