破晓 发表于 2016-8-18 16:30:20

Sass的表达式和控制命令

来源:http://www.w3cplus.com/preprocessor/sass-basics-control-directives-expressions.html<p></p><div><br></div><div><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">如果你深入地使用过 Sass,那么一定会接触过 mixin。一个独立的 mixin 往往聚合了大量的控制指令来实现复杂的功能。</p><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">在本文中我们就将这些控制指令和表达式做一些讲解和实践。也许你在开发中不见得会用到它们,但是熟悉一下这些指令,可能会有意想不到的收获!</p><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">if()</code></h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">if()</code>&nbsp;是 Sass 的一个内建函数,与之相似的&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@if</code>&nbsp;则是一个内建指令。<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">if()</code>&nbsp;用来做条件判断并返回特定值,示例如下:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@mixin</span> test(<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$condition</span>) {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$color</span><span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">:</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">if</span>(<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$condition</span>, blue, red);
    <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">color:</span><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$color</span>
}

.firstClass {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> test(<span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">true</span>);
}

.secondClass {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> test(<span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">false</span>);
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">编译结果:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.firstClass</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> blue;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.secondClass</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> red;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">在上例中,<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">if()</code>&nbsp;函数内的三个参数分别代表:测试条件,测试成功返回值,测试失败返回值(除了&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">false</code>&nbsp;和&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">null</code>&nbsp;之外的所有测试条件都被视为测试成功)。如果使用数字作为上述示例的条件,同样会返回测试成功的值:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">.firstClass {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> test(<span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);">1</span>);
}
</code></pre><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">@if</code></h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@if</code>&nbsp;后跟一个表达式,如果表达式的结果为&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">true</code>,则返回特定的样式,示例如下:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@mixin</span> txt(<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span>) {
    <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">color:</span> white;
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@if</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span> == bold { font-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">weight:</span> bold;}
}

.txt1 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(none);
}

.txt2 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(bold);
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">编译结果:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.txt1</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.txt2</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> bold;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">此外,我们可以使用&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@if ... @else if ... @else</code>&nbsp;结构来处理多个条件,示例如下:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@mixin</span> txt(<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span>) {
    <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">color:</span> white;
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@if</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span> == bold { font-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">weight:</span> bold;}
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@else</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">if</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span> == light { font-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">weight:</span> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);">100</span>;}
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@else</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">if</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$weight</span> == heavy { font-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">weight:</span> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);">900</span>;}
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@else</span> { font-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">weight:</span> normal;}
}

.txt1 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(bold);
}

.txt2 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(light);
}

.txt3 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(heavy);
}

.txt4 {
<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(none);
}

.txt5 {
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@include</span> txt(normal)
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">编译结果:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.txt1</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> bold;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.txt2</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">100</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.txt3</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">900</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.txt4</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> normal;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.txt5</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">font-weight</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> normal;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">这里的&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">.txt4</code>&nbsp;和&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">.txt5</code>&nbsp;是用来向各位演示&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@if</code>&nbsp;的解析机制。在&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">.txt1</code>&nbsp;中,如果不传入&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">bold</code>,那么就不会生成&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">font-weight</code>&nbsp;相关的样式。</p><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">@for</code></h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;指令常用于循环输出。<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;有两种使用方式:<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">from start through end</code>&nbsp;和&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">from start to end</code>,两者的区别在于,前者遍历的范围是&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);"></code>, 而后者的遍历范围是&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);"></code>。在&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;循环中使用一个固定变量来替代遍历到的元素。如果你想实现从大到小的遍历,只需让&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">start</code>&nbsp;大于&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">end</code>&nbsp;即可。下面是&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;的一个简单示例:</p><pre class="bash" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="bash" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">@<span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">for</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$i</span> from 1 through 4 {
    .col-<span class="comment" style="box-sizing: inherit; color: rgb(88, 110, 117); font-style: italic;">#{$i} { width: 100/4 * $i + %;}</span>
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">使用上面的这个例子,我们可以创建一套简单的栅格系统,编译结果如下:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.col-1</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
<span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">width</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">25</span></span>%;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.col-2</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
<span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">width</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">50</span></span>%;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.col-3</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
<span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">width</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">75</span></span>%;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.col-4</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
<span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">width</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="number" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="number" style="box-sizing: inherit;">100</span></span>%;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">从上面的示例中,我们综合了 Sass 的循环、变量、计算等多重功能。</p><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">@each</code></h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@each</code>&nbsp;指令同样可以用于循环输出,和&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;的差别在于,<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@each</code>&nbsp;通过遍历&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">list</code>&nbsp;或者&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">map</code>&nbsp;实现循环输出:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@each</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$usr</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">in</span> bob, john, bill, mike {
    .<span class="comment" style="box-sizing: inherit; color: rgb(88, 110, 117); font-style: italic;">#{$usr}-avatar {</span>
      background-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">image:</span> url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);">'/img/<span class="subst" style="box-sizing: inherit; color: rgb(203, 75, 22);"><span class="subst" style="box-sizing: inherit;">#{<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="variable" style="box-sizing: inherit;">$usr</span></span>}</span></span>.png'</span>);
   }
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@each</code>&nbsp;后面的&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">$usr</code>&nbsp;变量用于保存每次遍历到的元素,然后使用差值语法(<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">#{var}</code>)来拼接正确的图片路径,编译结果如下:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.bob-avatar</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-image</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="function" style="box-sizing: inherit;"><span class="function" style="box-sizing: inherit;">url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="string" style="box-sizing: inherit;">"/img/bob.png"</span></span>)</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.john-avatar</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-image</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="function" style="box-sizing: inherit;"><span class="function" style="box-sizing: inherit;">url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="string" style="box-sizing: inherit;">"/img/john.png"</span></span>)</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.bill-avatar</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-image</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="function" style="box-sizing: inherit;"><span class="function" style="box-sizing: inherit;">url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="string" style="box-sizing: inherit;">"/img/bill.png"</span></span>)</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.mike-avatar</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-image</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> <span class="function" style="box-sizing: inherit;"><span class="function" style="box-sizing: inherit;">url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);"><span class="string" style="box-sizing: inherit;">"/img/mike.png"</span></span>)</span></span>;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">如果遍历的对象是一个&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">map</code>,那么我们就可以使用两个变量来存储元素的&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">key</code>&nbsp;和&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">value</code>,示例如下:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$ppl</span><span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">:</span> ( <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">usr1:</span>bob, <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">usr2:</span>john, <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">usr3:</span>bill, <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">usr4:</span>mike );

<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@each</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$key</span>, <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$usr</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">in</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$ppl</span>{
    .<span class="comment" style="box-sizing: inherit; color: rgb(88, 110, 117); font-style: italic;">#{$usr}-avatar {</span>
      background-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">image:</span> url(<span class="string" style="box-sizing: inherit; color: rgb(42, 161, 152);">'/img/<span class="subst" style="box-sizing: inherit; color: rgb(203, 75, 22);"><span class="subst" style="box-sizing: inherit;">#{<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="variable" style="box-sizing: inherit;">$usr</span></span>}</span></span>.png'</span>);
    }
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">此外,针对多个列表的遍历,我们也可以使用多个参数来保存相应的元素:</p><pre class="ruby" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="ruby" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$alt</span><span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">:</span> alert, yellow, red;

<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$sub</span><span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">:</span> submit, white, green;

<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$bck</span><span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">:</span> back, blue, transparent;

<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">@each</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$type</span>, <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$txt</span>, <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$back</span> <span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">in</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$alt</span>,<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$sub</span>,<span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$bck</span> {
    .<span class="comment" style="box-sizing: inherit; color: rgb(88, 110, 117); font-style: italic;">#{$type}-button {</span>
      <span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">color:</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$txt</span>;
      background-<span class="symbol" style="box-sizing: inherit; color: rgb(203, 75, 22);">color:</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$back</span>;
    }
}
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">编译结果如下:</p><pre class="css" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="css" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="class" style="box-sizing: inherit;">.alert-button</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> yellow;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> red;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.submit-button</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> white;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> green;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>

<span class="class" style="box-sizing: inherit;">.back-button</span> <span class="rules" style="box-sizing: inherit;"><span class="rules" style="box-sizing: inherit;">{
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> blue;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
    <span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit; color: rgb(181, 137, 0);"><span class="rule" style="box-sizing: inherit;"><span class="attribute" style="box-sizing: inherit;">background-color</span></span></span><span class="rule" style="box-sizing: inherit;">:<span class="value" style="box-sizing: inherit;"><span class="value" style="box-sizing: inherit;"> transparent;</span></span></span><span class="value" style="box-sizing: inherit;"></span></span>
<span class="rule" style="box-sizing: inherit;"><span class="rule" style="box-sizing: inherit;">}</span></span></span><span class="rule" style="box-sizing: inherit;"></span></span>
</code></pre><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em;">@while</code></h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@while</code>&nbsp;指令也可以用于循环输出,它后面跟一个表达式,如果表达式结果为&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">false</code>,则停止循环体。下面使用&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@while</code>&nbsp;来重写上述的&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@for</code>&nbsp;示例:</p><pre class="bash" style="box-sizing: inherit; overflow: auto; font-family: monospace, monospace; padding: 0.5em; color: rgb(131, 148, 150); margin-bottom: 20px; line-height: 20px; background: rgb(0, 43, 54);"><code class="bash" style="box-sizing: inherit; font-family: monospace, monospace; font-size: 1em; display: block; padding: 0.5em; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$x</span>:1;

@<span class="keyword" style="box-sizing: inherit; color: rgb(133, 153, 0);">while</span> <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$x</span> &lt; 13 {
    .col-<span class="comment" style="box-sizing: inherit; color: rgb(88, 110, 117); font-style: italic;">#{$x} { width: 100/12 * $x;}</span>
    <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$x</span>: <span class="variable" style="box-sizing: inherit; color: rgb(181, 137, 0);">$x</span> + 1;
};
</code></pre><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">在上面的示例中,我们用一个表达式来控制&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@while</code>&nbsp;指令的执行,表达式中有一个变量&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">$x</code>,该变量一方面用于插值计算,另一方面在循环体内执行累加,最终当&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">$x &lt; 13</code>&nbsp;的结果为&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">false</code>&nbsp;时,程序就会推出&nbsp;<code style="box-sizing: inherit; font-family: Menlo, Monaco, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace; font-size: 1em; padding: 1px 3px; margin-right: 3px; margin-left: 3px; border: 1px solid rgb(204, 204, 204); background: rgb(221, 221, 221);">@while</code>&nbsp;循环。</p><h2 style="box-sizing: inherit; margin-bottom: 15px; font-size: 1.4em; color: rgb(0, 163, 207); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(221, 221, 221); padding-bottom: 5px; font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">总结</h2><p style="box-sizing: inherit; margin-bottom: 25px; font-size: 16px; line-height: 30px; color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Sass 的诸多特性让前端开发变得无比轻松,虽然 Sass 中的指令很强大,但是如果不是构建一个大象框架,往往并不能感觉到其中的美妙。随着对 Sass 的了解越来越深入,总有一天你会用到这些指令的 ^_^。</p><blockquote style="box-sizing: inherit; padding: 15px 20px; margin-bottom: 25px; border-left-width: 5px; border-left-style: solid; border-left-color: rgb(0, 163, 207); color: rgb(64, 64, 64); font-family: &quot;Microsoft YaHei&quot;, 微软雅黑, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; line-height: 20px; background: rgb(246, 246, 246);"><p style="box-sizing: inherit; font-size: 16px; line-height: 30px;">本文根据<a href="http://www.sitepoint.com/author/rdawson/" target="_blank" style="box-sizing: inherit; color: rgb(41, 180, 240); text-decoration: none; background: transparent;">@Reggie Dawson</a>的《<a href="http://www.sitepoint.com/sass-basics-control-directives-expressions/" target="_blank" style="box-sizing: inherit; color: rgb(41, 180, 240); text-decoration: none; background: transparent;">Sass Basics: Control Directives and Expressions</a>》所译,整个译文带有我们自己的理解与思想,如果译得不好或有不对之处还请同行朋友指点。如需转载此译文,需注明英文出处:<a href="http://www.sitepoint.com/sass-basics-control-directives-expressions/" target="_blank" style="box-sizing: inherit; color: rgb(41, 180, 240); text-decoration: none; background: transparent;">http://www.sitepoint.com/sass-basics-control-directives-expressions/</a>。</p></blockquote></div>
页: [1]
查看完整版本: Sass的表达式和控制命令