跳至内容
本页内容

插值

Stylus 支持使用 {} 字符包围表达式进行插值,然后该表达式成为标识符的一部分。例如,-webkit-{'border' + '-radius'} 计算为 -webkit-border-radius

一个很好的用例是使用供应商前缀扩展属性。

vendor(prop, args)
  -webkit-{prop} args
  -moz-{prop} args
  {prop} args

border-radius()
  vendor('border-radius', arguments)

box-shadow()
  vendor('box-shadow', arguments)

button
  border-radius 1px 2px / 3px 4px
vendor(prop, args)
  -webkit-{prop} args
  -moz-{prop} args
  {prop} args

border-radius()
  vendor('border-radius', arguments)

box-shadow()
  vendor('box-shadow', arguments)

button
  border-radius 1px 2px / 3px 4px

生成

button {
  -webkit-border-radius: 1px 2px / 3px 4px;
  -moz-border-radius: 1px 2px / 3px 4px;
  border-radius: 1px 2px / 3px 4px;
}
button {
  -webkit-border-radius: 1px 2px / 3px 4px;
  -moz-border-radius: 1px 2px / 3px 4px;
  border-radius: 1px 2px / 3px 4px;
}

选择器插值

插值也适用于选择器。例如,我们可能迭代为表格中的前 5 行分配 height 属性,如下所示

table
  for row in 1 2 3 4 5
    tr:nth-child({row})
      height: 10px * row
table
  for row in 1 2 3 4 5
    tr:nth-child({row})
      height: 10px * row

生成

table tr:nth-child(1) {
  height: 10px;
}
table tr:nth-child(2) {
  height: 20px;
}
table tr:nth-child(3) {
  height: 30px;
}
table tr:nth-child(4) {
  height: 40px;
}
table tr:nth-child(5) {
  height: 50px;
}
table tr:nth-child(1) {
  height: 10px;
}
table tr:nth-child(2) {
  height: 20px;
}
table tr:nth-child(3) {
  height: 30px;
}
table tr:nth-child(4) {
  height: 40px;
}
table tr:nth-child(5) {
  height: 50px;
}

您还可以通过构建字符串并将它们就地插值来将多个选择器组合到一个变量中

mySelectors = '#foo,#bar,.baz'

{mySelectors}
  background: #000
mySelectors = '#foo,#bar,.baz'

{mySelectors}
  background: #000

生成

#foo,
#bar,
.baz {
  background: #000;
}
#foo,
#bar,
.baz {
  background: #000;
}