我为全屏模式提出了一个“响应式”解决方案:
只能在某些断点上启用的全屏模式。通过这种方式,模态将在更宽的(桌面)屏幕上显示“正常”,在更小的(平板电脑或移动)屏幕上显示全屏,给人一种原生应用程序的感觉。
为Bootstrap 3和Bootstrap 4实现。默认情况下包含在Bootstrap 5中。
引导程序 v5
Bootstrap 5中默认包含全屏模式:https ://getbootstrap.com/docs/5.0/components/modal/#fullscreen-modal
引导程序 v4
以下通用代码应该可以工作:
.modal {
padding: 0 !important; // override inline padding-right added from js
}
.modal .modal-dialog {
width: 100%;
max-width: none;
height: 100%;
margin: 0;
}
.modal .modal-content {
height: 100%;
border: 0;
border-radius: 0;
}
.modal .modal-body {
overflow-y: auto;
}
通过包含下面的 scss 代码,它会生成以下需要添加到.modal
元素的类:
+---------------------+---------+---------+---------+---------+---------+
| | xs | sm | md | lg | xl |
| | <576px | ≥576px | ≥768px | ≥992px | ≥1200px |
+---------------------+---------+---------+---------+---------+---------+
|.modal-fullscreen | 100% | default | default | default | default |
+---------------------+---------+---------+---------+---------+---------+
|.modal-fullscreen-sm | 100% | 100% | default | default | default |
+---------------------+---------+---------+---------+---------+---------+
|.modal-fullscreen-md | 100% | 100% | 100% | default | default |
+---------------------+---------+---------+---------+---------+---------+
|.modal-fullscreen-lg | 100% | 100% | 100% | 100% | default |
+---------------------+---------+---------+---------+---------+---------+
|.modal-fullscreen-xl | 100% | 100% | 100% | 100% | 100% |
+---------------------+---------+---------+---------+---------+---------+
scss代码为:
@mixin modal-fullscreen() {
padding: 0 !important; // override inline padding-right added from js
.modal-dialog {
width: 100%;
max-width: none;
height: 100%;
margin: 0;
}
.modal-content {
height: 100%;
border: 0;
border-radius: 0;
}
.modal-body {
overflow-y: auto;
}
}
@each $breakpoint in map-keys($grid-breakpoints) {
@include media-breakpoint-down($breakpoint) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
.modal-fullscreen#{$infix} {
@include modal-fullscreen();
}
}
}
Codepen 上的演示:https ://codepen.io/andreivictor/full/MWYNPBV/
引导程序 v3
根据之前对该主题的回复(@Chris J、@kkarli),以下通用代码应该可以工作:
.modal {
padding: 0 !important; // override inline padding-right added from js
}
.modal .modal-dialog {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.modal .modal-content {
height: auto;
min-height: 100%;
border: 0 none;
border-radius: 0;
box-shadow: none;
}
如果要使用响应式全屏模式,请使用以下需要添加到.modal
元素的类:
.modal-fullscreen-md-down
- 对于小于1200px
.
.modal-fullscreen-sm-down
- 对于小于922px
.
.modal-fullscreen-xs-down
- 模式是全屏的屏幕小于768px
。
看看下面的代码:
/* Extra small devices (less than 768px) */
@media (max-width: 767px) {
.modal-fullscreen-xs-down {
padding: 0 !important;
}
.modal-fullscreen-xs-down .modal-dialog {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.modal-fullscreen-xs-down .modal-content {
height: auto;
min-height: 100%;
border: 0 none;
border-radius: 0;
box-shadow: none;
}
}
/* Small devices (less than 992px) */
@media (max-width: 991px) {
.modal-fullscreen-sm-down {
padding: 0 !important;
}
.modal-fullscreen-sm-down .modal-dialog {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.modal-fullscreen-sm-down .modal-content {
height: auto;
min-height: 100%;
border: 0 none;
border-radius: 0;
box-shadow: none;
}
}
/* Medium devices (less than 1200px) */
@media (max-width: 1199px) {
.modal-fullscreen-md-down {
padding: 0 !important;
}
.modal-fullscreen-md-down .modal-dialog {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.modal-fullscreen-md-down .modal-content {
height: auto;
min-height: 100%;
border: 0 none;
border-radius: 0;
box-shadow: none;
}
}
Codepen 上提供了演示:https ://codepen.io/andreivictor/full/KXNdoO 。
那些使用 Sass 作为预处理器的人可以利用以下 mixin:
@mixin modal-fullscreen() {
padding: 0 !important; // override inline padding-right added from js
.modal-dialog {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.modal-content {
height: auto;
min-height: 100%;
border: 0 none;
border-radius: 0;
box-shadow: none;
}
}