Arahan @if dalam SASS
Jika kita perlu gaya tertentu
diterapkan hanya apabila syarat-syarat
spesifik dipenuhi, maka kita menggunakan
arahan @if, yang mengembalikan sebarang
nilai, kecuali false dan null:
Pertimbangkan contoh berikut:
#main-button {
@if 2 - 1 == 1 {
color: green;
}
@if (2+3) < 3 {
color: orange;
}
@if null {
color: red;
}
}
Hasil kompilasi:
#main-button {
color: green;
}
Juga, apabila meletakkan syarat selepas @if,
beberapa @else if boleh digunakan
dan satu ungkapan @else.
$var: 20px;
a {
@if $var == 10px {
color: blue;
} @else if $var == 25px {
color: red;
} @else if $var == 10px+10px {
color: green;
} @else {
color: black;
}
}
Sekarang mari kita lihat fail styles.css:
a {
color: green;
}
Beritahu, apakah hasil kompilasi kod berikut:
$size: 20px;
$font-size: 14px;
body {
@if $size == $font-size {
background-color: #7e7ed8;
} @else if $size > $font-size {
color: #f1bbbb;
} @else {
color: #000000;
}
}
Beritahu, apakah hasil kompilasi kod berikut:
$size: 20px;
$font-size: 14px;
.active {
@if $size < $font-size {
color: red;
} @else if $size > $font-size {
color: green;
} @else {
color: #000000;
}
}
.not-active {
@if $size == $font-size {
display:block ;
} @else {
display: none;
}
}