Files
freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/sass/use-if-and-else-to-add-logic-to-your-styles.md
Oliver Eyton-Williams ee1e8abd87 feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.8 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dbe367417b2b2512bb8 使用 @if 和 @else 为你的样式添加逻辑 0 301463 use-if-and-else-to-add-logic-to-your-styles

--description--

Sass 中的@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-strokemixin,它接受一个参数$valmixin应使用@if@else if@else检查以下条件:

light - 1px solid black
medium - 3px solid black
heavy - 6px solid black

如果 $val 不是 lightmedium 或者 heavyborder 应该设置为 none

--hints--

你应该声明一个名为border-strokemixin,它有一个名为$val的参数。

assert(code.match(/@mixin\s+?border-stroke\s*?\(\s*?\$val\s*?\)\s*?{/gi));

mixin应该有一个@if语句来检查$val是否很轻,并将border设置为 1px 纯黑色。

assert(
  code.match(
    /@if\s+?\$val\s*?===?\s*?light\s*?{\s*?border\s*?:\s*?1px\s+?solid\s+?black\s*?;\s*?}/gi
  )
);

mixin应该有一个@else if语句来检查$val是否中等,并设置border为3px 纯黑色。

assert(
  code.match(
    /@else\s+?if\s+?\$val\s*?===?\s*?medium\s*?{\s*?border\s*?:\s*?3px\s+?solid\s+?black\s*?;\s*?}/gi
  )
);

mixin应该有一个@else if语句来检查$val是否很重,并设置border为6px 纯黑色。

assert(
  code.match(
    /@else\s+?if\s+?\$val\s*?===?\s*?heavy\s*?{\s*?border\s*?:\s*?6px\s+?solid\s+?black\s*?;\s*?}/gi
  )
);

mixin应该有一个@else语句来将border设置为 none。

assert(code.match(/@else\s*?{\s*?border\s*?:\s*?none\s*?;\s*?}/gi));

--seed--

--seed-contents--

<style type='text/scss'>



  #box {
    width: 150px;
    height: 150px;
    background-color: red;
    @include border-stroke(medium);
  }
</style>

<div id="box"></div>

--solutions--

<style type='text/scss'>
  @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>