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支持and、or和not。可以使用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}- 可以用括号明确
and和or的优先级。在复杂条件下,使用括号有助于保持可读性和安全性。
字符串操作——拼接与插值
在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频道。