內插
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;
}