responsive media

原文来自:snippets.barretlee.com,只是为了自己学习收集特意fork了一遍。如有侵权,联系删除:i@webcliwn.net

1
2
3
4
5
$breakpoints: (
'small': 767px,
'medium': 992px,
'large': 1200px
) !default;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/// Mixin to manage responsive breakpoints
/// @author Hugo Giraudel
/// @param {String} $breakpoint - Breakpoint name
/// @require $breakpoints
@mixin respond-to($breakpoint) {
// If the key exists in the map
@if map-has-key($breakpoints, $breakpoint) {
// Prints a media query based on the value
@media (min-width: map-get($breakpoints, $breakpoint)) {
@content;
}
}

// If the key doesn't exist in the map
@else {
@warn "Unfortunately, no value could be retrieved from `#{$breakpoint}`. "
+ "Available breakpoints are: #{map-keys($breakpoints)}.";
}
}

使用:

1
2
3
4
5
6
7
.selector {
color: red;

@include respond-to('small') {
color: blue;
}
}

结果:

1
2
3
4
5
6
7
8
.selector {
color: red;
}
@media (min-width: 767px) {
.selector {
color: blue;
}
}