我在 Bootstrap 之外使用它。您应该能够将它与 Bootstrap 或任何其他框架一起使用,从而为您的媒体查询提供更大的灵活性。
// Extra map functions by Hugo Giraudel
@function map-deep-get($map, $keys...) {
@each $key in $keys {
$map: map-get($map, $key);
}
@return $map;
}
@function map-has-keys($map, $keys...) {
@each $key in $keys {
@if not map-has-key($map, $key) {
@return false;
}
}
@return true;
}
@function map-has-nested-keys($map, $keys...) {
@each $key in $keys {
@if not map-has-key($map, $key) {
@return false;
}
$map: map-get($map, $key);
}
@return true;
}
这些是Hugo Giraudel 编写的额外地图功能。 map-deep-get 基本上是一个简化的嵌套 map-get 函数。 map-has-keys 就像 map-has-key 一样,它是 sass 内置的,但会检查多个键。 map-has-nested-keys 通过检查嵌套键对此进行了扩展。这对于这种方法至关重要。我肯定会研究他构建的额外 Sass 函数。我很容易找到它们的用途。
// Map
$sizes: (
null: (
breakpoint: 0,
container: 100%
),
xs: (
breakpoint: 480px,
container: 464px
),
sm: (
breakpoint: 768px,
container: 750px
),
md: (
breakpoint: 992px,
container: 970px
),
lg: (
breakpoint: 1200px,
container: 1170px
)
);
这是一个简单的断点图。我通常将其用作项目中所有设置的基本地图,因此我将在其中包含基本字体大小等内容。
// Breakpoint mixin
@mixin break($screen-min: null, $screen-max: null, $orientation: null) {
$min: $screen-min;
$max: $screen-max;
$o: $orientation;
$query: unquote("only screen");
@if $min != null and $min != "" {
@if map-has-nested-keys($base, sizes, $screen-min) {
$min: map-deep-get($base, sizes, $screen-min, breakpoint);
}
@else {
$min: $screen-min;
}
@if is-number($min) {
$query: append($query, unquote("and (min-width: #{$min})"));
}
}
@if $max != null and $max != "" {
@if map-has-nested-keys($base, sizes, $screen-max) {
$max: map-deep-get($base, sizes, $screen-max, breakpoint);
}
@else {
$max: $screen-max;
}
@if is-number($max) {
$query: append($query, unquote("and (max-width: #{$max})"));
}
}
@if $orientation == landscape or $orientation == portrait {
$o: $orientation;
$query: append($query, unquote("and (orientation: #{$o})"));
}
@else {
$o: null;
}
@media #{$query} {
@content;
}
};
这是混合。您可以使用尺寸映射中的键(xs、sm、md、lg)作为前两个参数,也可以使用自定义值(如 30em)。第三个参数接受横向或纵向。如果需要,您甚至可以自定义 make l = Landscape 和 p = Portrait 的 mixin。
此外,如果您只想要一个方向,例如,您可以传递参数(null、null、landscape)。
为了清楚起见,这里有一些例子:
@include break(null, md, landscape) {
...
}
@include break(null, null, landscape) {
...
}
@include break(md) {
...
}
@include break(null, md) {
...
}
@include break(480px) {
...
}
输出:
@media only screen and (max-width: 992px) and (orientation: landscape) {
...
}
@media only screen and (orientation: landscape) {
...
}
@media only screen and (min-width: 992px) {
...
}
@media only screen and (max-width: 992px) {
...
}
@media only screen and (min-width: 480px) {
...
}