查看 1181
回复 0
提取 Bootstrap 5 中的 自适应(Breakpoints)
逆风天

16

主题

0

回帖

105

积分
发表于 2021-12-18 09:37:57
显示全部楼层 阅读模式
本帖最后由 逆风天 于 2021-12-18 10:00 编辑
提取 Bootstrap 5 中的 自适应(Breakpoints)

在最新的 Bootstrap 5.* 中 他们加入了 6种分辨率的自适应样式。
当你不想自己写,又想用他们的自适应,又不想用其他的组件样式,那怎么办呢?
下面我们来一步一步的提取他们的自适应样式。

注意:本教程,需要在你有一定的 scss 基础的情况下进行!

原料:需要在 Bootstrap官网 下载 最新 源码包

步骤:
1、新建一个 _breakpoints.scss 备用。

2、获取分辨率属性,在文件 scss\_variables.scss 中搜索,如下:
  1. $grid-breakpoints: (
  2.   xs: 0,
  3.   sm: 576px,
  4.   md: 768px,
  5.   lg: 992px,
  6.   xl: 1200px,
  7.   xxl: 1400px
  8. ) !default;
复制代码
复制进 _breakpoints.scss。

3、再获取自适应的 mixin,在文件 scss\mixins\_breakpoints.scss ,将内部的代码复制到我们新建备用的 _breakpoints.scss 中。看起来如下:
  1. // scss-docs-start grid-breakpoints
  2. $grid-breakpoints: (
  3.   xs: 0,
  4.   sm: 576px,
  5.   md: 768px,
  6.   lg: 992px,
  7.   xl: 1200px,
  8.   xxl: 1400px
  9. ) !default;

  10. // Breakpoint viewport sizes and media queries.
  11. //
  12. // Breakpoints are defined as a map of (name: minimum width), order from small to large:
  13. //
  14. //    (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
  15. //
  16. // The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.

  17. // Name of the next breakpoint, or null for the last breakpoint.
  18. //
  19. //    >> breakpoint-next(sm)
  20. //    md
  21. //    >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  22. //    md
  23. //    >> breakpoint-next(sm, $breakpoint-names: (xs sm md lg xl))
  24. //    md
  25. @function breakpoint-next($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
  26.   $n: index($breakpoint-names, $name);
  27.   @if not $n {
  28.     @error "breakpoint `#{$name}` not found in `#{$breakpoints}`";
  29.   }
  30.   @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
  31. }

  32. // Minimum breakpoint width. Null for the smallest (first) breakpoint.
  33. //
  34. //    >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  35. //    576px
  36. @function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
  37.   $min: map-get($breakpoints, $name);
  38.   @return if($min != 0, $min, null);
  39. }

  40. // Maximum breakpoint width.
  41. // The maximum value is reduced by 0.02px to work around the limitations of
  42. // `min-` and `max-` prefixes and viewports with fractional widths.
  43. // See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
  44. // Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
  45. // See https://bugs.webkit.org/show_bug.cgi?id=178261
  46. //
  47. //    >> breakpoint-max(md, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  48. //    767.98px
  49. @function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
  50.   $max: map-get($breakpoints, $name);
  51.   @return if($max and $max > 0, $max - .02, null);
  52. }

  53. // Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
  54. // Useful for making responsive utilities.
  55. //
  56. //    >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  57. //    ""  (Returns a blank string)
  58. //    >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  59. //    "-sm"
  60. @function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
  61.   @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
  62. }

  63. // Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
  64. // Makes the @content apply to the given breakpoint and wider.
  65. @mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
  66.   $min: breakpoint-min($name, $breakpoints);
  67.   @if $min {
  68.     @media (min-width: $min) {
  69.       @content;
  70.     }
  71.   } @else {
  72.     @content;
  73.   }
  74. }

  75. // Media of at most the maximum breakpoint width. No query for the largest breakpoint.
  76. // Makes the @content apply to the given breakpoint and narrower.
  77. @mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
  78.   $max: breakpoint-max($name, $breakpoints);
  79.   @if $max {
  80.     @media (max-width: $max) {
  81.       @content;
  82.     }
  83.   } @else {
  84.     @content;
  85.   }
  86. }

  87. // Media that spans multiple breakpoint widths.
  88. // Makes the @content apply between the min and max breakpoints
  89. @mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
  90.   $min: breakpoint-min($lower, $breakpoints);
  91.   $max: breakpoint-max($upper, $breakpoints);

  92.   @if $min != null and $max != null {
  93.     @media (min-width: $min) and (max-width: $max) {
  94.       @content;
  95.     }
  96.   } @else if $max == null {
  97.     @include media-breakpoint-up($lower, $breakpoints) {
  98.       @content;
  99.     }
  100.   } @else if $min == null {
  101.     @include media-breakpoint-down($upper, $breakpoints) {
  102.       @content;
  103.     }
  104.   }
  105. }

  106. // Media between the breakpoint's minimum and maximum widths.
  107. // No minimum for the smallest breakpoint, and no maximum for the largest one.
  108. // Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
  109. @mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
  110.   $min:  breakpoint-min($name, $breakpoints);
  111.   $next: breakpoint-next($name, $breakpoints);
  112.   $max:  breakpoint-max($next);

  113.   @if $min != null and $max != null {
  114.     @media (min-width: $min) and (max-width: $max) {
  115.       @content;
  116.     }
  117.   } @else if $max == null {
  118.     @include media-breakpoint-up($name, $breakpoints) {
  119.       @content;
  120.     }
  121.   } @else if $min == null {
  122.     @include media-breakpoint-down($next, $breakpoints) {
  123.       @content;
  124.     }
  125.   }
  126. }
复制代码


4、下面就可以直接使用了。使用方法如下:
  1. .custom-class {
  2.   display: none;
  3. }
  4. @include media-breakpoint-up(sm) {
  5.   .custom-class {
  6.     display: block;
  7.   }
  8. }
复制代码

5、其他。支持的分辨率如下:
  1. // 分辨率宽度小于576px      <576px
  2. @include media-breakpoint-up(xs) { ... }
  3. // 分辨率宽度大于等于576px      ≥576px
  4. @include media-breakpoint-up(sm) { ... }
  5. // 分辨率宽度大于等于768px      ≥768px
  6. @include media-breakpoint-up(md) { ... }
  7. // 分辨率宽度大于等于992px      ≥992px
  8. @include media-breakpoint-up(lg) { ... }
  9. // 分辨率宽度大于等于1200px      ≥1200px
  10. @include media-breakpoint-up(xl) { ... }
  11. // 分辨率宽度大于等于1400px      ≥1400px
  12. @include media-breakpoint-up(xxl) { ... }
复制代码

6、还有 media-breakpoint-down、media-breakpoint-between、media-breakpoint-only等这些,这个就你自己去实践了,当然你也可以去 Bootstrap官网 查看文档使用。

祝,愉快使用!

您需要登录后才可以回帖 登录 立即注册
QQ 快速回复 返回列表