数字

数字可以带单位也可以不带单位,当带单位的数字进行计算时,单位也会随之计算。

列表

你可以这样取列表的值。

  1. @debug nth(10px 12px 16px, 2); // 12px
  2. @debug nth([line1, line2, line3], -1); // line3

map

必须使用括号,使用带引号的键值而不是不带引号的。

获得键值

  1. $font-weights: ("regular": 400, "medium": 500, "bold": 700);
  2. @debug map-get($font-weights, "medium"); // 500
  3. @debug map-get($font-weights, "extra-bold"); // null

map-merge() 函数执合并两个 map 中的所有键值对,不会修改原始列表。

  1. $light-weights: ("lightest": 100, "light": 300);
  2. $heavy-weights: ("medium": 500, "bold": 700);
  3. @debug map-merge($light-weights, $heavy-weights);
  4. // (
  5. // "lightest": 100,
  6. // "light": 300,
  7. // "medium": 500,
  8. // "bold": 700

你仍然可以通过将新映射分配给同一变量来更新 map,这通常用于函数和 mixin 。

  1. $prefixes-by-browser: ("firefox": moz, "safari": webkit, "ie": ms);
  2. @mixin add-browser-prefix($browser, $prefix) {
  3. $prefixes-by-browser: map-merge($prefixes-by-browser, ($browser: $prefix));
  4. }
  5. @include add-browser-prefix("opera", o);
  6. @debug $prefixes-by-browser;
  7. // ("firefox": moz, "safari": webkit, "ie": ms, "opera": o)

布尔值

if() 函数选择返回一个值,如果它的参数是 true 与另一个如果它的参数是 false

  1. @debug if(true, 10px, 30px); // 10px
  2. @debug if(false, 10px, 30px); // 30px

空字符串、空列表在 sass 中都是 true。

null

  1. @mixin app-background($color) {
  2. #{if(&, '&.app-background', '.app-background')} {
  3. background-color: $color;
  4. color: rgba(#fff, 0.75);
  5. }
  6. }
  7. @include app-background(#036);
  8. .sidebar {
  9. @include app-background(#c6538c);
  10. }

function

不能直接将函数作为值,可以将函数的名称传递给 get-function() 以将函数作为值。一旦有了函数值,就可以将它传递给 call() 函数来调用它。这对于编写调用其他函数的高阶函数很有用。

  1. @function remove-where($list, $condition) {
  2. $new-list: ();
  3. $separator: list-separator($list);
  4. @each $element in $list {
  5. @if not call($condition, $element) {
  6. $new-list: append($new-list, $element, $separator: $separator);
  7. }
  8. }
  9. @return $new-list;
  10. }
  11. $fonts: Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif;
  12. content {
  13. @function contains-helvetica($string) {
  14. @return str-index($string, "Helvetica");
  15. }
  16. font-family: remove-where($fonts, get-function("contains-helvetica"));
  17. }

参考

【1】Sass 官网文档