我想在同一个scs中使用多个include。例如
.section-ptb {
padding-top: 130px;
padding-bottom: 130px;
@include desktop {
padding-top: 80px;
padding-bottom: 80px;
}
@include tablet {
padding-top: 80px;
padding-bottom: 80px;
}
@include mobole {
padding-top: 80px;
padding-bottom: 80px;
}
}
经常对多个@include很无聊。有什么办法可以减少代码,我想使用
.section-ptb {
padding-top: 130px;
padding-bottom: 130px;
@include desktop , @include tablet, @include mobole {
padding-top: 80px;
padding-bottom: 80px;
}
}
但这不是有效的SCSS。请告诉我另一种减少代码的方法。
答案 0 :(得分:1)
如@karthick所述,尚不支持动态包含。对于您的情况,我认为只有一个mixin来处理所有媒体查询是很有意义的,例如:
SCSS
// map holding breakpoint values
$breakpoints: (
mobile : 0px,
tablet : 680px,
desktop: 960px
);
// mixin to print out media queries (based on map keys passed)
@mixin media($keys...){
@each $key in $keys {
@media (min-width: map-get($breakpoints, $key)){
@content
}
}
}
.section-ptb {
padding-top: 130px;
padding-bottom: 130px;
// pass the key(s) of the media queries you want to print
@include media(mobile, tablet, desktop){
padding-top: 80px;
padding-bottom: 80px;
}
}
CSS输出
.section-ptb {
padding-top: 130px;
padding-bottom: 130px;
}
@media (min-width: 0px) {
.section-ptb {
padding-top: 80px;
padding-bottom: 80px;
}
}
@media (min-width: 680px) {
.section-ptb {
padding-top: 80px;
padding-bottom: 80px;
}
}
@media (min-width: 960px) {
.section-ptb {
padding-top: 80px;
padding-bottom: 80px;
}
}
答案 1 :(得分:1)
你可以这样使用:
SCSS
@mixin media($keys...) {
@each $key in $keys {
@if ($key == phone) {
@include phone {
@content
}
} @else if ($key == tablet) {
@include tablet {
@content
}
} @else if ($key == desktop) {
@include desktop {
@content
}
}
}
}
用法
@include media(phone, tablet, desktop) {
// your scss code
}
@include media(tablet, desktop) {
// your scss code
}
@include media(phone) {
// your scss code
}
// and so on...