Css 较少混合中的条件参数 问题
我创建了以下mixin:Css 较少混合中的条件参数 问题,css,less,Css,Less,我创建了以下mixin: .type(@style;@mb) { & when (@style = hero) { margin-bottom: @mb; font-size: 2.625rem; line-height: 1.238095238; } } 现在,这正是我想要的。我遇到的问题是,有时我想声明一个@mb值,但很多时候我不会。在这些情况下,我希望回退到每个@style参数的预定义值 例如: 对于hero,@m
.type(@style;@mb) {
& when (@style = hero) {
margin-bottom: @mb;
font-size: 2.625rem;
line-height: 1.238095238;
}
}
现在,这正是我想要的。我遇到的问题是,有时我想声明一个@mb
值,但很多时候我不会。在这些情况下,我希望回退到每个@style
参数的预定义值
例如:
- 对于
,hero
默认为@mb
边距底部:1.25rem代码>
- 对于
,页面
默认为@mb
页边距底部:1.125rem代码>
- 等
预期结果 预期结果如下:
.sample-class-01 { .type(hero); }
.sample-class-02 { .type(page,0); }
我将得到以下输出:
.sample-class-01 {
margin-bottom: 1.25rem;
font-size: 2.625rem;
line-height: 1.238095238;
}
.sample-class-02 {
margin-bottom: 0;
font-size: 2rem;
line-height: 1.3125;
}
如何创建此混音?类似以下内容:
.type(@style; @mb: '') {
& when (@style = hero) {
font-size: 42px; // 42px
line-height: 52px; // 52px
& when not (@mb = '') { margin-bottom: @mb; }
& when (@mb = '') { margin-bottom: 1.25rem; }
}
& when (@style = footer) {
font-size: 22px; // 42px
line-height: 32px; // 52px
& when not (@mb = '') { margin-bottom: @mb; }
& when (@mb = '') { margin-bottom: 1.125rem; }
}
}
.sample-class-1 { .type(hero); }
.sample-class-2 { .type(hero,0); }
.sample-class-3 { .type(hero,3rem); }
.sample-class-4 { .type(footer); }
.sample-class-5 { .type(footer,0); }
.sample-class-6 { .type(footer,3rem); }
应编译为:
.sample-class-1 {
font-size: 42px;
line-height: 52px;
margin-bottom: 1.25rem;
}
.sample-class-2 {
font-size: 42px;
line-height: 52px;
margin-bottom: 0;
}
.sample-class-3 {
font-size: 42px;
line-height: 52px;
margin-bottom: 3rem;
}
.sample-class-4 {
font-size: 22px;
line-height: 32px;
margin-bottom: 1.125rem;
}
.sample-class-5 {
font-size: 22px;
line-height: 32px;
margin-bottom: 0;
}
.sample-class-6 {
font-size: 22px;
line-height: 32px;
margin-bottom: 3rem;
}
类似这样的内容:
.type(@style; @mb: '') {
& when (@style = hero) {
font-size: 42px; // 42px
line-height: 52px; // 52px
& when not (@mb = '') { margin-bottom: @mb; }
& when (@mb = '') { margin-bottom: 1.25rem; }
}
& when (@style = footer) {
font-size: 22px; // 42px
line-height: 32px; // 52px
& when not (@mb = '') { margin-bottom: @mb; }
& when (@mb = '') { margin-bottom: 1.125rem; }
}
}
.sample-class-1 { .type(hero); }
.sample-class-2 { .type(hero,0); }
.sample-class-3 { .type(hero,3rem); }
.sample-class-4 { .type(footer); }
.sample-class-5 { .type(footer,0); }
.sample-class-6 { .type(footer,3rem); }
应编译为:
.sample-class-1 {
font-size: 42px;
line-height: 52px;
margin-bottom: 1.25rem;
}
.sample-class-2 {
font-size: 42px;
line-height: 52px;
margin-bottom: 0;
}
.sample-class-3 {
font-size: 42px;
line-height: 52px;
margin-bottom: 3rem;
}
.sample-class-4 {
font-size: 22px;
line-height: 32px;
margin-bottom: 1.125rem;
}
.sample-class-5 {
font-size: 22px;
line-height: 32px;
margin-bottom: 0;
}
.sample-class-6 {
font-size: 22px;
line-height: 32px;
margin-bottom: 3rem;
}
只需对每个样式集进行mixin专门化/重载:
.type(hero, @mb: 1.25rem) {
margin-bottom: @mb;
font-size: 2.625rem;
line-height: 1.238095238;
}
.type(not-a-hero, @mb: 2.22rem) {
margin-bottom: @mb;
font-size: 3.333rem;
line-height: 4.444444444;
}
// etc.
参考号:
.type(hero, @mb: 1.25rem) {
margin-bottom: @mb;
font-size: 2.625rem;
line-height: 1.238095238;
}
.type(not-a-hero, @mb: 2.22rem) {
margin-bottom: @mb;
font-size: 3.333rem;
line-height: 4.444444444;
}
// etc.
参考号: