@if 和 @else
@if 规则的写法是 @if <expression> { ... },它控制其代码块是否被评估(包括是否将任何样式作为 CSS 发出)。该表达式通常返回 true 或 false——如果表达式返回 true,则评估代码块;如果表达式返回 false,则不评估。
@if 规则的写法是 @if <expression> { ... },它控制其代码块是否被评估(包括是否将任何样式作为 CSS 发出)。该表达式通常返回 true 或 false——如果表达式返回 true,则评估代码块;如果表达式返回 false,则不评估。
@use "sass:math";
@mixin avatar($size, $circle: false) {
width: $size;
height: $size;
@if $circle {
border-radius: math.div($size, 2);
}
}
.square-av {
@include avatar(100px, $circle: false);
}
.circle-av {
@include avatar(100px, $circle: true);
}
@use "sass:math"
@mixin avatar($size, $circle: false)
width: $size
height: $size
@if $circle
border-radius: math.div($size, 2)
.square-av
@include avatar(100px, $circle: false)
.circle-av
@include avatar(100px, $circle: true)
.square-av {
width: 100px;
height: 100px;
}
.circle-av {
width: 100px;
height: 100px;
border-radius: 50px;
}
@else
一个 @if 规则可以选择性地后跟一个 @else 规则,其写法是 @else { ... }。如果 @if 表达式返回 false,则评估此规则的代码块。
$light-background: #f2ece4;
$light-text: #036;
$dark-background: #6b717f;
$dark-text: #d2e1dd;
@mixin theme-colors($light-theme: true) {
@if $light-theme {
background-color: $light-background;
color: $light-text;
} @else {
background-color: $dark-background;
color: $dark-text;
}
}
.banner {
@include theme-colors($light-theme: true);
body.dark & {
@include theme-colors($light-theme: false);
}
}
$light-background: #f2ece4
$light-text: #036
$dark-background: #6b717f
$dark-text: #d2e1dd
@mixin theme-colors($light-theme: true)
@if $light-theme
background-color: $light-background
color: $light-text
@else
background-color: $dark-background
color: $dark-text
.banner
@include theme-colors($light-theme: true)
body.dark &
@include theme-colors($light-theme: false)
.banner {
background-color: #f2ece4;
color: #036;
}
body.dark .banner {
background-color: #6b717f;
color: #d2e1dd;
}
条件表达式可以包含布尔运算符(and、or、not)。
@else if
你也可以通过编写 @else if <expression> { ... } 来选择是否评估 @else 规则的代码块。如果你这样做,只有当前面的 @if 表达式返回 false 并且 @else if 的表达式返回 true 时,才会评估该代码块。
实际上,你可以在一个 @if 后面链接任意数量的 @else if。该链中第一个表达式返回 true 的代码块将被评估,而其他代码块则不会。如果链的末尾有一个普通的 @else,如果所有其他代码块都失败,它的代码块将被评估。
@use "sass:math";
@mixin triangle($size, $color, $direction) {
height: 0;
width: 0;
border-color: transparent;
border-style: solid;
border-width: math.div($size, 2);
@if $direction == up {
border-bottom-color: $color;
} @else if $direction == right {
border-left-color: $color;
} @else if $direction == down {
border-top-color: $color;
} @else if $direction == left {
border-right-color: $color;
} @else {
@error "Unknown direction #{$direction}.";
}
}
.next {
@include triangle(5px, black, right);
}
@use "sass:math"
@mixin triangle($size, $color, $direction)
height: 0
width: 0
border-color: transparent
border-style: solid
border-width: math.div($size, 2)
@if $direction == up
border-bottom-color: $color
@else if $direction == right
border-left-color: $color
@else if $direction == down
border-top-color: $color
@else if $direction == left
border-right-color: $color
@else
@error "Unknown direction #{$direction}."
.next
@include triangle(5px, black, right)
.next {
height: 0;
width: 0;
border-color: transparent;
border-style: solid;
border-width: 2.5px;
border-left-color: black;
}
真值和假值
在任何允许 true 或 false 的地方,你也可以使用其他值。false 和 null 值是假值,这意味着 Sass 认为它们表示虚假并导致条件失败。所有其他值都被认为是真值,因此 Sass 认为它们的作用类似于 true 并导致条件成功。
例如,如果你想检查一个字符串是否包含空格,你只需编写 string.index($string, " ")。string.index() 函数如果找不到字符串则返回 null,否则返回一个数字。
有些语言认为比
false 和 null 更多的值是假值。Sass 不是其中之一!空字符串、空列表和数字 0 在 Sass 中都是真值。