SASS 中的運算子

SASS 中的運算子

本文說明 SASS 中的各種運算子。

我們將通過實際範例來說明 SASS 的運算子。

YouTube Video

SASS 中的運算子

SASS 運算子包括數值運算、比較運算、邏輯運算以及字串操作。

算術運算(加、減、乘、除、模)

SASS 支援標準算術運算,但除法 / 容易與 CSS 的 / 混淆,因此建議使用 math.div()。數值可以有單位,只有在單位相容時才能進行運算。

以下是簡單算術運算以及 math 模組使用的範例。

 1@use "sass:math";
 2
 3$base: 16px;
 4$gutter: 8px;
 5
 6/* Addition and multiplication (allowed since both units are px) */
 7.container {
 8  width: $base + $gutter * 3; // -> 40px
 9}
10
11/* Use math.div() for division (/ is deprecated and triggers a warning) */
12.half {
13  padding: math.div($base, 2); // -> 8px
14}
  • 這段程式碼示範了使用 px 單位的加法與乘法,以及用 math.div() 進行除法。
  • 不相容單位(如 10px + 2em)會導致錯誤或非預期的結果。請統一單位,或利用 unit()unitless() 函數輔助。

單位的處理與自動轉換

SASS 會管理帶單位的數值,計算結果的單位遵循一般物理法則。例如 px/px 計算後會變為無單位的數值。使用 unit()math.div() 後,請留意單位的管理。

以下範例說明如何處理帶單位與無單位數值的混合運算。

1$width: 100px;
2$ratio: 0.5;
3
4.box {
5  /* Multiply width by a ratio (multiplying by a unitless value is fine) */
6  width: $width * $ratio; // -> 50px
7}
8
9/* If you want to remove the unit, combine it with math.div() for the operation */
  • 混合帶單位與無單位的數值進行乘法運算沒問題,但除法時需留意結果單位(或可能變為無單位)。

比較運算子(==, !=, <, >, <=, >=)

SASS 支援比較運算子,可用於條件判斷(@if)及 mixin 中的分支。顏色和列表等複雜型別的比較有其獨特規則,尤其注意顏色比較在不同色域下可能會有差異。

下列範例展示了兩個數值的比較及樣式的分支。

1$breakpoint: 768px;
2
3.container {
4  @if $breakpoint >= 768px {
5    max-width: 1200px;
6  } @else {
7    max-width: 100%;
8  }
9}
  • 比較運算子可以搭配 @if 使用,製作有條件及響應式的樣式規則。

邏輯運算(and、or、not)

SASS 支援 andornot。用 and/or 連結多個條件。可用括號明確指定優先級和運算順序。

以下範例展示如何使用邏輯運算子處理複雜條件。

 1$mobile: false;
 2$logged-in: true;
 3
 4.notice {
 5  @if $logged-in and not $mobile {
 6    display: block;
 7  } @else {
 8    display: none;
 9  }
10}
  • andor 的優先級可用括號明確化。複雜條件時使用括號可提升易讀性與安全性。

字串操作 —— 串接與插值

在 SASS 中可用 +#{} 插值實現字串串接。使用 + 串接時,左邊字串是否有引號會影響結果,因此建議使用可預測的 #{} 插值方式。

以下是字串串接與插值的範例。

 1@use "sass:string";
 2
 3$base: "icon-";
 4$variant: string.unquote("arrow");
 5
 6.icon {
 7  /* Concatenation using + (old style) */
 8  &--old { content: $base + $variant; } // -> icon-arrow
 9
10  /* Interpolation is more reliable */
11  &--new { content: "#{$base}#{$variant}"; } // -> icon-arrow
12}
13
14/* Interpolation is useful for generating selector names, URLs, or animation names */
15@keyframes slide-#{$variant} {
16  from { transform: translateX(0); }
17  to   { transform: translateX(100%); }
18}
  • 使用 #{} 插值可以避免引號問題及不預期的空白字元插入。

顏色運算(已不建議使用)

過去,SASS 支援『色彩運算』,允許你將數值加到顏色或從顏色中減去數值。然而,色彩運算現在已經被棄用,建議你透過 sass:color 模組中的函式來處理顏色。

以下範例展示顏色數值運算已不建議使用,並推薦替代用法。

 1/* Deprecated (old code example – no longer works or recommended) */
 2/* background: $base-color + 10; */
 3
 4@use "sass:color";
 5
 6$base: #336699;
 7
 8.bg {
 9  /* For example, to increase lightness, use color.scale() or color.adjust() */
10  /* Safe manipulation based on color space */
11  background: color.scale($base, $lightness: 20%);
12  /* Or make it lighter by mixing white */
13  background: color.mix(white, $base, 20%);
14}
  • 如果你想處理顏色,請使用 sass:color 模組中的函式。

括號與運算優先級

運算中可用括號控制計算順序。括號內的內容會優先於外層運算執行。複雜運算式中加上括號可提升易讀性。

以下範例透過括號明確運算順序。

1@debug (1 + 2) * 3; // 9
2@debug 1 + 2 * 3;   // 7 (multiplication first)
  • 與數學運算同理,運算優先級規則適用,建議用括號確保計算照預期順序進行。

總結

在 SASS 使用運算子時,請注意程式碼的未來相容性與可讀性。在複雜運算式中,用括號明確運算順序,並優先考慮易讀性。

您可以在我們的 YouTube 頻道上使用 Visual Studio Code 來跟隨上述文章一起學習。 請也查看我們的 YouTube 頻道。

YouTube Video