3.4 KiB
3.4 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7dbe367417b2b2512bb8 | Use @if and @else to Add Logic To Your Styles | 0 | 301463 | 使用 @if 和 @else 为你的样式添加逻辑 |
Description
@if
指令对于测试特定情况非常有用--它的工作方式与 JavaScript中的if
语句类似。
@mixin make-bold($bool) {
@if $bool == true {
font-weight: bold;
}
}
类似 JavaScript,你可以在 Sass 中使用@else if
和@else
添加更多条件:
@mixin text-effect($val) {
@if $val == danger {
color: red;
}
@else if $val == alert {
color: yellow;
}
@else if $val == success {
color: green;
}
@else {
color: black;
}
}
Instructions
border-stroke
的mixin
,它接受一个参数$val
。mixin
应使用@if
,@else if
和@else
检查以下条件:
light - 1px solid black
medium - 3px solid black
heavy - 6px solid black
如果 $val
不是 light
、medium
或者 heavy
,border 应该设置为 none
。
Tests
tests:
- text: 你应该声明一个名为<code>border-stroke</code>的<code>mixin</code>,它有一个名为<code>$val</code>的参数。
testString: assert(code.match(/@mixin\s+?border-stroke\s*?\(\s*?\$val\s*?\)\s*?{/gi));
- text: <code>mixin</code>应该有一个<code>@if</code>语句来检查<code>$val</code>是否很轻,并将<code>border</code>设置为 1px 纯黑色。
testString: assert(code.match(/@if\s+?\$val\s*?===?\s*?light\s*?{\s*?border\s*?:\s*?1px\s+?solid\s+?black\s*?;\s*?}/gi));
- text: <code>mixin</code>应该有一个<code>@else if</code>语句来检查<code>$val</code>是否中等,并设置<code>border</code>为3px 纯黑色。
testString: assert(code.match(/@else\s+?if\s+?\$val\s*?===?\s*?medium\s*?{\s*?border\s*?:\s*?3px\s+?solid\s+?black\s*?;\s*?}/gi));
- text: <code>mixin</code>应该有一个<code>@else if</code>语句来检查<code>$val</code>是否很重,并设置<code>border</code>为6px 纯黑色。
testString: assert(code.match(/@else\s+?if\s+?\$val\s*?===?\s*?heavy\s*?{\s*?border\s*?:\s*?6px\s+?solid\s+?black\s*?;\s*?}/gi));
- text: <code>mixin</code>应该有一个<code>@else</code>语句来将<code>border</code>设置为 none。
testString: assert(code.match(/@else\s*?{\s*?border\s*?:\s*?none\s*?;\s*?}/gi));
Challenge Seed
<style type='text/sass'>
#box {
width: 150px;
height: 150px;
background-color: red;
@include border-stroke(medium);
}
</style>
<div id="box"></div>
Solution
<style type='text/sass'>
@mixin border-stroke($val) {
@if $val == light {
border: 1px solid black;
}
@else if $val == medium {
border: 3px solid black;
}
@else if $val == heavy {
border: 6px solid black;
}
@else {
border: none;
}
}
#box {
width: 150px;
height: 150px;
background-color: red;
@include border-stroke(medium);
}
</style>
<div id="box"></div>