我正在创建一个mixin,它调整$element
的{{1}}样式以生成特定于页面的CSS。 (背景:有四个页面有不同的配色方案)。
不使用mixin(使用if语句):
$property
错误:
@mixin themify($element, $property, $color-light: false) {
@if $color-light == "true" {
$pages: home forestGreen, about darkOrange, work dodgerBlue, contact fireBrick;
}
@else {
$pages: home darkGreen, about orange, work royalBlue, contact crimson;
}
@each $page in $pages {
.page--#{nth($page, 1)} .#{$element} {
#{$property}: nth($page, 2);
}
}
}
/* Call the mixin */
@include themify(site-nav, background-color, $color-light: true);
在if语句之前添加error/style.scss (Line 47 of css/ui/_misc.scss: Undefined variable "$pages".)
会有所帮助。为什么呢?
答案 0 :(得分:23)
您需要在$pages
子句之外定义默认@if
。
这是一个范围问题...... @if
子句的范围比你的mixin窄......所以内部定义的任何内容都是该范围的私有。
试试这样:
@mixin themify($element, $property, $color-light: false) {
$pages: ();
@if $color-light == true { // boolean check not string
$pages: home forestGreen, about darkOrange, work dodgerBlue, contact fireBrick;
}
@else {
$pages: home darkGreen, about orange, work royalBlue, contact crimson;
}
@each $page in $pages {
.page--#{nth($page, 1)} .#{$element} {
#{$property}: nth($page, 2);
}
}
}
/* Call the mixin */
@include themify(site-nav, background-color, $color-light: true);
答案 1 :(得分:1)
消息错误可能令人困惑,但您有语法错误。它应该是@else
而不是else
。
答案 2 :(得分:0)
我做了一个小函数来正确检查布尔值(至少对我自己来说)
http://www.sassmeister.com/gist/a72a18f259c9c18ab773300b197bd08a
// ----
// libsass (v3.3.6)
// ----
// CHECK BOOLEAN VALUE
@function bool($value: false) {
@if $value == false or
$value == "" or
$value == "false" or
$value == 'false' or
$value == 0 {
@return false;
}
@return true;
}
// Lets test
$var1: false;
@if bool($var1) == true {
/* This value is true */
} @else {
/* This value is false */
}