本帖最后由 逆风天 于 2021-12-18 10:00 编辑
提取 Bootstrap 5 中的 自适应(Breakpoints)
在最新的 Bootstrap 5.* 中 他们加入了 6种分辨率的自适应样式。
当你不想自己写,又想用他们的自适应,又不想用其他的组件样式,那怎么办呢?
下面我们来一步一步的提取他们的自适应样式。
注意:本教程,需要在你有一定的 scss 基础的情况下进行!
原料:需要在
Bootstrap官网 下载
最新 源码包。
步骤:
1、新建一个 _breakpoints.scss 备用。
2、获取分辨率属性,在文件 scss\_variables.scss 中搜索,如下:
- $grid-breakpoints: (
- xs: 0,
- sm: 576px,
- md: 768px,
- lg: 992px,
- xl: 1200px,
- xxl: 1400px
- ) !default;
复制代码复制进 _breakpoints.scss。
3、再获取自适应的 mixin,在文件 scss\mixins\_breakpoints.scss ,将内部的代码复制到我们新建备用的 _breakpoints.scss 中。看起来如下:
- // scss-docs-start grid-breakpoints
- $grid-breakpoints: (
- xs: 0,
- sm: 576px,
- md: 768px,
- lg: 992px,
- xl: 1200px,
- xxl: 1400px
- ) !default;
- // Breakpoint viewport sizes and media queries.
- //
- // Breakpoints are defined as a map of (name: minimum width), order from small to large:
- //
- // (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
- //
- // The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.
- // Name of the next breakpoint, or null for the last breakpoint.
- //
- // >> breakpoint-next(sm)
- // md
- // >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
- // md
- // >> breakpoint-next(sm, $breakpoint-names: (xs sm md lg xl))
- // md
- @function breakpoint-next($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
- $n: index($breakpoint-names, $name);
- @if not $n {
- @error "breakpoint `#{$name}` not found in `#{$breakpoints}`";
- }
- @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
- }
- // Minimum breakpoint width. Null for the smallest (first) breakpoint.
- //
- // >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
- // 576px
- @function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
- $min: map-get($breakpoints, $name);
- @return if($min != 0, $min, null);
- }
- // Maximum breakpoint width.
- // The maximum value is reduced by 0.02px to work around the limitations of
- // `min-` and `max-` prefixes and viewports with fractional widths.
- // See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
- // Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
- // See https://bugs.webkit.org/show_bug.cgi?id=178261
- //
- // >> breakpoint-max(md, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
- // 767.98px
- @function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
- $max: map-get($breakpoints, $name);
- @return if($max and $max > 0, $max - .02, null);
- }
- // Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
- // Useful for making responsive utilities.
- //
- // >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
- // "" (Returns a blank string)
- // >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
- // "-sm"
- @function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
- @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
- }
- // Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
- // Makes the @content apply to the given breakpoint and wider.
- @mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
- $min: breakpoint-min($name, $breakpoints);
- @if $min {
- @media (min-width: $min) {
- @content;
- }
- } @else {
- @content;
- }
- }
- // Media of at most the maximum breakpoint width. No query for the largest breakpoint.
- // Makes the @content apply to the given breakpoint and narrower.
- @mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
- $max: breakpoint-max($name, $breakpoints);
- @if $max {
- @media (max-width: $max) {
- @content;
- }
- } @else {
- @content;
- }
- }
- // Media that spans multiple breakpoint widths.
- // Makes the @content apply between the min and max breakpoints
- @mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
- $min: breakpoint-min($lower, $breakpoints);
- $max: breakpoint-max($upper, $breakpoints);
- @if $min != null and $max != null {
- @media (min-width: $min) and (max-width: $max) {
- @content;
- }
- } @else if $max == null {
- @include media-breakpoint-up($lower, $breakpoints) {
- @content;
- }
- } @else if $min == null {
- @include media-breakpoint-down($upper, $breakpoints) {
- @content;
- }
- }
- }
- // Media between the breakpoint's minimum and maximum widths.
- // No minimum for the smallest breakpoint, and no maximum for the largest one.
- // Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
- @mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
- $min: breakpoint-min($name, $breakpoints);
- $next: breakpoint-next($name, $breakpoints);
- $max: breakpoint-max($next);
- @if $min != null and $max != null {
- @media (min-width: $min) and (max-width: $max) {
- @content;
- }
- } @else if $max == null {
- @include media-breakpoint-up($name, $breakpoints) {
- @content;
- }
- } @else if $min == null {
- @include media-breakpoint-down($next, $breakpoints) {
- @content;
- }
- }
- }
复制代码
4、下面就可以直接使用了。使用方法如下:
- .custom-class {
- display: none;
- }
- @include media-breakpoint-up(sm) {
- .custom-class {
- display: block;
- }
- }
复制代码
5、其他。支持的分辨率如下:
- // 分辨率宽度小于576px <576px
- @include media-breakpoint-up(xs) { ... }
- // 分辨率宽度大于等于576px ≥576px
- @include media-breakpoint-up(sm) { ... }
- // 分辨率宽度大于等于768px ≥768px
- @include media-breakpoint-up(md) { ... }
- // 分辨率宽度大于等于992px ≥992px
- @include media-breakpoint-up(lg) { ... }
- // 分辨率宽度大于等于1200px ≥1200px
- @include media-breakpoint-up(xl) { ... }
- // 分辨率宽度大于等于1400px ≥1400px
- @include media-breakpoint-up(xxl) { ... }
复制代码
6、还有 media-breakpoint-down、media-breakpoint-between、media-breakpoint-only等这些,这个就你自己去实践了,当然你也可以去
Bootstrap官网 查看文档使用。
祝,愉快使用!