您绝对可以进一步改进您的工作流程!
使用SASS@mixin
, @include
, @content
和定期的variables
您可以设置一个环境,在其中“内联”创建媒体查询,或者如果愿意,也可以在常规选择器内部创建。
这是我的媒体查询混合:
(不用介意愚蠢的变量名和占位符断点)
// Breakpoints
$mq-tiny: 5em;
$mq-small: 10em;
$mq-medium: 15em;
$mq-large: 20em;
$mq-huge: 25em;
$mq-crazy: 30em;
@mixin mq($size, $direction: min-width, $media-type: only all) {
@if $size == tiny { @media #{$media-type} and (#{$direction}: $mq-tiny) { @content; } }
@else if $size == small { @media #{$media-type} and (#{$direction}: $mq-small) { @content; } }
@else if $size == medium { @media #{$media-type} and (#{$direction}: $mq-medium) { @content; } }
@else if $size == large { @media #{$media-type} and (#{$direction}: $mq-large) { @content; } }
@else if $size == huge { @media #{$media-type} and (#{$direction}: $mq-huge) { @content; } }
@else if $size == crazy { @media #{$media-type} and (#{$direction}: $mq-crazy) { @content; } }
@else { @media #{$media-type} and (#{$direction}: $size) { @content; } }
}
这是如何使用它的示例:
SCSS:
.selector {
width: 100px;
@include mq(large) {
width: 1000px;
}
}
CSS 输出:
.selector {
width: 100px
}
@media only all and (min-width: 20em) {
.selector {
width: 1000px
}
}
请注意,您不需要使用宽度变量名称。你可以通过1000px
代替large
如果你想。
您可能还注意到了中的可选参数@mixin
; $direction
and $media-type
。这些参数默认为min-width
and only all
分别,但如果你通过它们@include
它们只会针对该特定元素而改变。
带有其他参数的示例:
SCSS:
.selector {
@include mq(1000px, min-height) {
width: 100px;
}
}
CSS 输出:
@media only all and (min-height: 1000px) {
.selector {
width: 100px
}
}
希望这可以帮助!
Edit:
这是一支笔 http://codepen.io/Mest/pen/mAhxv?editors=110如果你想玩玩它。