sass:list

兼容性:
Dart Sass
since 1.23.0
LibSass
Ruby Sass

Only Dart Sass currently supports loading built-in modules with @use. Users of other implementations must call functions using their global names instead.

💡 Fun fact:

在 Sass 中,每个 映射 都算作一个列表,其中包含每个键/值对的二元素列表。例如,(1: 2, 3: 4) 算作 (1 2, 3 4)。所以所有这些功能也适用于映射!

¥In Sass, every map counts as a list that contains a two-element list for each key/value pair. For example, (1: 2, 3: 4) counts as (1 2, 3 4). So all these functions work for maps as well!

单个值也算作列表。所有这些函数都将 1px 视为包含值 1px 的列表。

¥Individual values also count as lists. All these functions treat 1px as a list that contains the value 1px.

list.append($list, $val, $separator: auto)
append($list, $val, $separator: auto) //=> list

返回 $list 的副本,并在末尾添加 $val

¥Returns a copy of $list with $val added to the end.

如果 $separatorcommaspaceslash,则返回的列表分别以逗号分隔、空格分隔或斜杠分隔。如果是 auto(默认),则返回的列表将使用与 $list 相同的分隔符(如果 $list 没有分隔符,则使用 space)。不允许使用其他值。

¥If $separator is comma, space, or slash, the returned list is comma-separated, space-separated, or slash-separated, respectively. If it’s auto (the default), the returned list will use the same separator as $list (or space if $list doesn’t have a separator). Other values aren’t allowed.

请注意,与 list.join() 不同,如果 $val 是一个列表,它会嵌套在返回的列表中,而不是将其所有元素添加到返回的列表中。

¥Note that unlike list.join(), if $val is a list it’s nested within the returned list rather than having all its elements added to the returned list.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.append(10px 20px, 30px); // 10px 20px 30px
@debug list.append((blue, red), green); // blue, red, green
@debug list.append(10px 20px, 30px 40px); // 10px 20px (30px 40px)
@debug list.append(10px, 20px, $separator: comma); // 10px, 20px
@debug list.append((blue, red), green, $separator: space); // blue red green
Playground

Sass Syntax

@use 'sass:list'

@debug list.append(10px 20px, 30px)  // 10px 20px 30px
@debug list.append((blue, red), green)  // blue, red, green
@debug list.append(10px 20px, 30px 40px)  // 10px 20px (30px 40px)
@debug list.append(10px, 20px, $separator: comma)  // 10px, 20px
@debug list.append((blue, red), green, $separator: space)  // blue red green
list.index($list, $value)
index($list, $value) //=> number | null

返回 $list$value索引

¥Returns the index of $value in $list.

如果 $value 没有出现在 $list 中,则返回 null。如果 $value$list 中出现多次,则返回其第一次出现的索引。

¥If $value doesn’t appear in $list, this returns null. If $value appears multiple times in $list, this returns the index of its first appearance.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.index(1px solid red, 1px); // 1
@debug list.index(1px solid red, solid); // 2
@debug list.index(1px solid red, dashed); // null
Playground

Sass Syntax

@use 'sass:list'

@debug list.index(1px solid red, 1px)  // 1
@debug list.index(1px solid red, solid)  // 2
@debug list.index(1px solid red, dashed)  // null
list.is-bracketed($list)
is-bracketed($list) //=> boolean

返回 $list 是否有方括号。

¥Returns whether $list has square brackets.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.is-bracketed(1px 2px 3px); // false
@debug list.is-bracketed([1px, 2px, 3px]); // true
Playground

Sass Syntax

@use 'sass:list'

@debug list.is-bracketed(1px 2px 3px)  // false
@debug list.is-bracketed([1px, 2px, 3px])  // true
list.join($list1, $list2, $separator: auto, $bracketed: auto)
join($list1, $list2, $separator: auto, $bracketed: auto) //=> list

返回一个新列表,其中包含 $list1 的元素,后跟 $list2 的元素。

¥Returns a new list containing the elements of $list1 followed by the elements of $list2.

⚠️ Heads up!

Because individual values count as single-element lists, it's possible to
use `list.join()` to add a value to the end of a list. However, *this is not
recommended*, since if that value is a list it will be concatenated, which
is probably not what you're expecting.

Use list.append() instead to add a single value to a list. Only use list.join() to combine two lists together into one.

如果 $separatorcommaspaceslash,则返回的列表分别以逗号分隔、空格分隔或斜杠分隔。如果是 auto(默认),则返回的列表将使用与 $list1(如果有分隔符)相同的分隔符,或者 $list2(如果有分隔符),或者 space。不允许使用其他值。

¥If $separator is comma, space, or slash, the returned list is comma-separated, space-separated, or slash-separated, respectively. If it’s auto (the default), the returned list will use the same separator as $list1 if it has a separator, or else $list2 if it has a separator, or else space. Other values aren’t allowed.

如果 $bracketedauto(默认),则返回的列表将被括起来(如果 $list1 是)。否则,如果 $bracketed真实的,则返回的列表将有方括号;如果 $bracketed 是 false,则返回的列表将没有括号。

¥If $bracketed is auto (the default), the returned list will be bracketed if $list1 is. Otherwise, the returned list will have square brackets if $bracketed is truthy and no brackets if $bracketed is falsey.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.join(10px 20px, 30px 40px); // 10px 20px 30px 40px
@debug list.join((blue, red), (#abc, #def)); // blue, red, #abc, #def
@debug list.join(10px, 20px); // 10px 20px
@debug list.join(10px, 20px, $separator: comma); // 10px, 20px
@debug list.join((blue, red), (#abc, #def), $separator: space); // blue red #abc #def
@debug list.join([10px], 20px); // [10px 20px]
@debug list.join(10px, 20px, $bracketed: true); // [10px 20px]
Playground

Sass Syntax

@use 'sass:list'

@debug list.join(10px 20px, 30px 40px)  // 10px 20px 30px 40px
@debug list.join((blue, red), (#abc, #def))  // blue, red, #abc, #def
@debug list.join(10px, 20px)  // 10px 20px
@debug list.join(10px, 20px, comma)  // 10px, 20px
@debug list.join((blue, red), (#abc, #def), space)  // blue red #abc #def
@debug list.join([10px], 20px)  // [10px 20px]
@debug list.join(10px, 20px, $bracketed: true)  // [10px 20px]
list.length($list)
length($list) //=> number

返回 $list 的长度。

¥Returns the length of $list.

这也可以返回映射中的对数。

¥This can also return the number of pairs in a map.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.length(10px); // 1
@debug list.length(10px 20px 30px); // 3
@debug list.length((width: 10px, height: 20px)); // 2
Playground

Sass Syntax

@use 'sass:list'

@debug list.length(10px)  // 1
@debug list.length(10px 20px 30px)  // 3
@debug list.length((width: 10px, height: 20px))  // 2
list.separator($list)
list-separator($list) //=> unquoted string

返回 $list 使用的分隔符的名称,可以是 spacecommaslash

¥Returns the name of the separator used by $list, either space, comma, or slash.

如果 $list 没有分隔符,则返回 space

¥If $list doesn’t have a separator, returns space.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.separator(1px 2px 3px); // space
@debug list.separator((1px, 2px, 3px)); // comma
@debug list.separator('Helvetica'); // space
@debug list.separator(()); // space
Playground

Sass Syntax

@use 'sass:list'

@debug list.separator(1px 2px 3px)  // space
@debug list.separator((1px, 2px, 3px))  // comma
@debug list.separator('Helvetica')  // space
@debug list.separator(())  // space
list.nth($list, $n)
nth($list, $n)

返回 $list索引 $n 处的元素。

¥Returns the element of $list at index $n.

如果 $n 为负数,则从 $list 末尾开始计数。如果索引 $n 处没有元素,则抛出错误。

¥If $n is negative, it counts from the end of $list. Throws an error if there is no element at index $n.

Playground

SCSS Syntax

@use 'sass:list';

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

Sass Syntax

@use 'sass:list'

@debug list.nth(10px 12px 16px, 2)  // 12px
@debug list.nth([line1, line2, line3], -1)  // line3
list.set-nth($list, $n, $value)
set-nth($list, $n, $value) //=> list

返回 $list 的副本,其中 索引 $n 处的元素替换为 $value

¥Returns a copy of $list with the element at index $n replaced with $value.

如果 $n 为负数,则从 $list 末尾开始计数。如果索引 $n 处不存在现有元素,则抛出错误。

¥If $n is negative, it counts from the end of $list. Throws an error if there is no existing element at index $n.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.set-nth(10px 20px 30px, 1, 2em); // 2em 20px 30px
@debug list.set-nth(10px 20px 30px, -1, 8em); // 10px, 20px, 8em
@debug list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto); // Helvetica, Arial, Roboto
Playground

Sass Syntax

@use 'sass:list'

@debug list.set-nth(10px 20px 30px, 1, 2em); // 2em 20px 30px
@debug list.set-nth(10px 20px 30px, -1, 8em); // 10px, 20px, 8em
@debug list.set-nth((Helvetica, Arial, sans-serif), 3, Roboto); // Helvetica, Arial, Roboto
list.slash($elements...) //=> list

返回包含 $elements 的斜杠分隔列表。

¥Returns a slash-separated list that contains $elements.

⚠️ Heads up!

This function is a temporary solution for creating slash-separated lists.
Eventually, they'll be written literally with slashes, as in
`1px / 2px / solid`, but for the time being [slashes are used for division]
so Sass can't use them for new syntax until the old syntax is removed.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.slash(1px, 50px, 100px); // 1px / 50px / 100px
Playground

Sass Syntax

@use 'sass:list'

@debug list.slash(1px, 50px, 100px)  // 1px / 50px / 100px
list.zip($lists...)
zip($lists...) //=> list

$lists 中的每个列表合并为一个子列表列表。

¥Combines every list in $lists into a single list of sub-lists.

返回列表中的每个元素都包含 $lists 中该位置的所有元素。返回的列表与 $lists 中最短的列表一样长。

¥Each element in the returned list contains all the elements at that position in $lists. The returned list is as long as the shortest list in $lists.

返回的列表始终以逗号分隔,子列表始终以空格分隔。

¥The returned list is always comma-separated and the sub-lists are always space-separated.

Playground

SCSS Syntax

@use 'sass:list';

@debug list.zip(10px 50px 100px, short mid long); // 10px short, 50px mid, 100px long
@debug list.zip(10px 50px 100px, short mid); // 10px short, 50px mid
Playground

Sass Syntax

@use 'sass:list'

@debug list.zip(10px 50px 100px, short mid long)  // 10px short, 50px mid, 100px long
@debug list.zip(10px 50px 100px, short mid)  // 10px short, 50px mid