模態(tài)彈框(Modal)
使用Bootstrap的JavaScript模式插件將對話框添加到站點中,用于燈箱、用戶通知或完全自定義的內(nèi)容。
工作原理
在開始使用Bootstrap互動視窗之前,請務(wù)必閱讀以下內(nèi)容,因為菜單項目在近期內(nèi)有修改。
- 互動視窗是用HTML、CSS和JavaScript構(gòu)建的。它們位于文件中任何其他內(nèi)容之上,并從body中刪除滾動,以便互動視窗的內(nèi)容滾動。
- 點擊互動視窗“backdrop”將自動關(guān)閉互動視窗。
- Bootstrap一次只支持一個互動視窗。不支持巢狀互動視窗,因為我們認為巢狀互動視窗用戶體驗不佳。
- 互動視窗使用position: fixed,在渲染呈現(xiàn)上會有點特別。盡可能將您的互動視窗HTML放置在頂級位置,以避免其他元素的潛在干擾。在另一個固定元素中內(nèi)加入一個.modal的時候,你可能會遇到問題。
- 由于position: fixed,在移動設(shè)備上使用互動視窗有一些附加說明。有關(guān)詳細信息,請參閱我們的瀏覽器支持。
- 依據(jù)HTML5定義語義的方式,autofocus HTML屬性對Bootstrap互動視窗沒有影響。要達到同樣的效果,請使用一些自定義JavaScript:the
autofocus
HTML attribute
var myModal = document.getElementById('myModal')
var myInput = document.getElementById('myInput')
myModal.addEventListener('shown.bs.modal', function () {
myInput.focus()
})
繼續(xù)閱讀demos和使用指南。
示例
動態(tài)視窗元件
下面是一個靜態(tài)動態(tài)視窗示例(意思是它的position和display已被復(fù)寫)。包括動態(tài)視窗標題、動態(tài)視窗主體(padding必要)和動態(tài)視窗頁腳(可選)。盡可能地包含動態(tài)視窗標題與移除按鈕,或提供另一個明確的移除操作。
<div class="modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
完整示例
通過點擊下面的按鈕切換動態(tài)視窗呈現(xiàn)。它將從頁面頂部向下滑動并淡入。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
靜態(tài)背景
當將背景設(shè)置為靜態(tài)時,互動視窗不會因為點擊背景而關(guān)閉。
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Launch static backdrop modal
</button>
<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
滾動長內(nèi)容
當使用者的動態(tài)視窗變得太長時,它們的滾動獨立于于頁面本身。透過下方示例了解:
除此之外您也可以透過在 .modal-dialog 中加入 .modal-dialog-scrollable 來創(chuàng)建一個 body 可滾動互動視窗。
<!-- Scrollable modal -->
<div class="modal-dialog modal-dialog-scrollable">
...
</div>
垂直居中
加入.modal-dialog-centered到.modal-dialog來使互動視窗垂直置中。
<!-- Vertically centered modal -->
<div class="modal-dialog modal-dialog-centered">
...
</div>
<!-- Vertically centered scrollable modal -->
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
...
</div>
工具提示和彈出窗口
Tooltips和popovers可以根據(jù)需要放置在動態(tài)視窗。當動態(tài)視窗關(guān)閉時,其中的任何工具提示和插件也將自動關(guān)閉。
<div class="modal-body">
<h5>Popover in a modal</h5>
<p>This <a href="#" role="button" class="btn btn-secondary popover-test" title="Popover title" data-bs-content="Popover body content is set in this attribute.">button</a> triggers a popover on click.</p>
<hr>
<h5>Tooltips in a modal</h5>
<p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href="#" class="tooltip-test" title="Tooltip">that link</a> have tooltips on hover.</p>
</div>
使用網(wǎng)格
透過在.modal-body中加入.container-fluid在互動視窗中使用Bootstrap網(wǎng)格系統(tǒng)。然后像其他任何地方一樣使用正常的網(wǎng)格系統(tǒng)class。
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-3 ms-auto">.col-md-3 .ms-auto</div>
<div class="col-md-2 ms-auto">.col-md-2 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-6 ms-auto">.col-md-6 .ms-auto</div>
</div>
<div class="row">
<div class="col-sm-9">
Level 1: .col-sm-9
<div class="row">
<div class="col-8 col-sm-6">
Level 2: .col-8 .col-sm-6
</div>
<div class="col-4 col-sm-6">
Level 2: .col-4 .col-sm-6
</div>
</div>
</div>
</div>
</div>
</div>
可變模態(tài)內(nèi)容
有數(shù)個按鈕要用來觸發(fā)相同互動視窗,但會導(dǎo)入不同內(nèi)容?使用event.relatedTarget和HTML data-bs-*屬性依據(jù)點擊按鈕來更改動態(tài)視窗的內(nèi)容。
下面是HTML和JavaScript的示例。有關(guān)relatedTarget的詳細信息,請參閱互動視窗的事件。
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal" data-bs-whatever="@getbootstrap">Open modal for @getbootstrap</button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form>
<div class="mb-3">
<label for="recipient-name" class="col-form-label">Recipient:</label>
<input type="text" class="form-control" id="recipient-name">
</div>
<div class="mb-3">
<label for="message-text" class="col-form-label">Message:</label>
<textarea class="form-control" id="message-text"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Send message</button>
</div>
</div>
</div>
</div>
var exampleModal = document.getElementById('exampleModal')
exampleModal.addEventListener('show.bs.modal', function (event) {
// Button that triggered the modal
var button = event.relatedTarget
// Extract info from data-bs-* attributes
var recipient = button.getAttribute('data-bs-whatever')
// If necessary, you could initiate an AJAX request here
// and then do the updating in a callback.
//
// Update the modal's content.
var modalTitle = exampleModal.querySelector('.modal-title')
var modalBodyInput = exampleModal.querySelector('.modal-body input')
modalTitle.textContent = 'New message to ' + recipient
modalBodyInput.value = recipient
})
Toggle between modals
在多個模態(tài)之間切換,巧妙地放置data-bs-target和data-bs-Toggle屬性。例如,可以在已打開的登錄模式中切換密碼重置模式。請注意,不能同時打開多個模態(tài)。這個方法只是在兩個單獨的模態(tài)之間切換。
<!-- First modal dialog -->
<div class="modal fade" id="modal" aria-hidden="true" aria-labelledby="..." tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
...
<div class="modal-footer">
<!-- Toogle to second dialog -->
<button class="btn btn-primary" data-bs-target="#modal2" data-bs-toggle="modal" data-bs-dismiss="modal">Open #modal2</button>
</div>
</div>
</div>
</div>
<!-- Second modal dialog -->
<div class="modal fade" id="modal2" aria-hidden="true" aria-labelledby="..." tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
...
<div class="modal-footer">
<!-- Toogle to first dialog, `data-bs-dismiss` attribute can be omitted - clicking on link will close dialog anyway -->
<a class="btn btn-primary" href="#modal" data-bs-toggle="modal" role="button">Open #modal</a>
</div>
</div>
</div>
</div>
<!-- Open first dialog -->
<a class="btn btn-primary" data-bs-toggle="modal" href="#modal" role="button">Open #modal</a>
更改動畫
$modal-fade-transform變數(shù)在互動視窗執(zhí)行淡入動畫前會先確定.modal-dialog的轉(zhuǎn)換狀態(tài),當互動視窗的淡入動畫結(jié)束時,$modal-show-transform變數(shù)會決定淡入動畫結(jié)束時.modal-dialog的轉(zhuǎn)換。
如果您想要一個放大的動畫,您可以設(shè)定$modal-fade-transform: scale(.8)
移除動畫
對于只要出現(xiàn)而不需要淡入淡出的互動視窗,你只需要在互動視窗中刪除.fade。
<div class="modal" tabindex="-1" aria-labelledby="..." aria-hidden="true">
...
</div>
動態(tài)高度
如果互動視窗的高度在打開時發(fā)生變化,則應(yīng)呼叫myModal.handleUpdate()以重新調(diào)整互動視窗的位置,以防滾動條出現(xiàn)。
可達性
請務(wù)必在.modal上添加aria-labelledby=“…”來引用互動視窗標題。另外,你也可以在.modal上使用aria-describedby來描述你的動態(tài)視窗。請注意你不需要加上role=“dialog”,因為bootstrap的JavaScript已經(jīng)有添加了。
嵌入YouTube視頻
在互動視窗上嵌入YouTube視頻需要額外的JavaScript以自動停止回放或其他更多功能。有關(guān)詳細信息,請參閱Stack Overflow的文章。 See this helpful Stack Overflow post for more information.
可選尺寸
互動視窗有三個可選的大小,透過.modal-dialog上的modifier Class啟用。這些尺寸會在某些斷點調(diào)整,以避免在較窄的viewport上出現(xiàn)水平卷軸。
Size | Class | Modal max-width |
---|---|---|
Small | .modal-sm |
300px |
Default | None | 500px |
Large | .modal-lg |
800px |
Extra large | .modal-xl |
1140px |
預(yù)設(shè)的互動視窗沒有“medium”這個類別的大小。
<div class="modal-dialog modal-xl">...</div>
<div class="modal-dialog modal-lg">...</div>
<div class="modal-dialog modal-sm">...</div>
全屏模式
另一個的覆蓋選項是跳出一個完全覆蓋用戶屏幕的互動視窗,可透過在.modal-dialog上放置修飾類別來啟用。
Class | Availability |
---|---|
.modal-fullscreen |
Always |
.modal-fullscreen-sm-down |
Below 576px |
.modal-fullscreen-md-down |
Below 768px |
.modal-fullscreen-lg-down |
Below 992px |
.modal-fullscreen-xl-down |
Below 1200px |
.modal-fullscreen-xxl-down |
Below 1400px |
<!-- Full screen modal -->
<div class="modal-dialog modal-fullscreen-sm-down">
...
</div>
Sass
Variables
$modal-inner-padding: $spacer;
$modal-footer-margin-between: .5rem;
$modal-dialog-margin: .5rem;
$modal-dialog-margin-y-sm-up: 1.75rem;
$modal-title-line-height: $line-height-base;
$modal-content-color: null;
$modal-content-bg: $white;
$modal-content-border-color: rgba($black, .2);
$modal-content-border-width: $border-width;
$modal-content-border-radius: $border-radius-lg;
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-content-border-width);
$modal-content-box-shadow-xs: $box-shadow-sm;
$modal-content-box-shadow-sm-up: $box-shadow;
$modal-backdrop-bg: $black;
$modal-backdrop-opacity: .5;
$modal-header-border-color: $border-color;
$modal-footer-border-color: $modal-header-border-color;
$modal-header-border-width: $modal-content-border-width;
$modal-footer-border-width: $modal-header-border-width;
$modal-header-padding-y: $modal-inner-padding;
$modal-header-padding-x: $modal-inner-padding;
$modal-header-padding: $modal-header-padding-y $modal-header-padding-x; // Keep this for backwards compatibility
$modal-sm: 300px;
$modal-md: 500px;
$modal-lg: 800px;
$modal-xl: 1140px;
$modal-fade-transform: translate(0, -50px);
$modal-show-transform: none;
$modal-transition: transform .3s ease-out;
$modal-scale-transform: scale(1.02);
Loop
Responsive fullscreen modals are generated via the $breakpoints
map
and a loop in scss/_modal.scss
.
@each $breakpoint in map-keys($grid-breakpoints) {
$infix: breakpoint-infix($breakpoint, $grid-breakpoints);
$postfix: if($infix != "", $infix + "-down", "");
@include media-breakpoint-down($breakpoint) {
.modal-fullscreen#{$postfix} {
width: 100vw;
max-width: none;
height: 100%;
margin: 0;
.modal-content {
height: 100%;
border: 0;
@include border-radius(0);
}
.modal-header {
@include border-radius(0);
}
.modal-body {
overflow-y: auto;
}
.modal-footer {
@include border-radius(0);
}
}
}
}
用法
互動視窗插件透過數(shù)據(jù)屬性或JavaScript切換您隱藏的內(nèi)容。它將.modal-open加到body以復(fù)寫預(yù)設(shè)的滾動行為,并生成一個.modal-backdrop來提供點擊區(qū)域,以便在點擊動態(tài)視窗外面時移除顯示的動態(tài)視窗。
通過數(shù)據(jù)屬性
啟動一個動態(tài)視窗而無需編寫JavaScript。在控制元素上(如按鈕)設(shè)置data-bs-toggle=“modal”,以及一個data-bs-target=“#foo”或href=“#foo”來指定特定的動態(tài)視窗進行切換。
<button type="button" data-bs-toggle="modal" data-bs-target="#myModal">Launch modal</button>
通過JavaScript
用一行JavaScript創(chuàng)建一個互動視窗:
var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
選項
可以透過數(shù)據(jù)屬性或JavaScript傳遞選項。對于數(shù)據(jù)屬性,將選項名稱附加到data-bs,如data-bs-backdrop=“”。
Name | Type | Default | Description |
---|---|---|---|
backdrop |
boolean or the string 'static' |
true |
包括模態(tài)背景元素。或者,為不在單擊時關(guān)閉模式的背景指定static。 |
keyboard |
boolean | true |
按escape鍵時關(guān)閉模式 |
focus |
boolean | true |
初始化時將焦點放在模態(tài)上。 |
方法
異步方法和轉(zhuǎn)換
所有API方法都是異步的,并開始轉(zhuǎn)換。轉(zhuǎn)換一開始就返回到調(diào)用方,但在轉(zhuǎn)換結(jié)束之前返回。此外,對轉(zhuǎn)換組件的方法調(diào)用將被忽略。 有關(guān)更多信息,請參閱我們的JavaScript文檔。
傳遞選項
將你的內(nèi)容啟用為互動視窗,接受一個選擇性的object選項。
var myModal = new bootstrap.Modal(document.getElementById('myModal'), {
keyboard: false
})
切換
手動切換動態(tài)視窗。在動態(tài)視窗實際顯示或隱藏之前返回給調(diào)用者(即在shown.bs.modal或hidden.bs.modal事件發(fā)生之前)。
myModal.toggle()
顯示
手動打開動態(tài)視窗。在動態(tài)視窗實際顯示之前返回給調(diào)用者(即在shown.bs.modal事件發(fā)生之前)。
myModal.show()
Also, you can pass a DOM element as an argument that can be received in the modal events (as the
relatedTarget
property).
var modalToggle = document.getElementById('toggleMyModal') // relatedTarget
myModal.show(modalToggle)
隱藏
手動隱藏動態(tài)視窗。在動態(tài)視窗實際隱藏之前返回給調(diào)用者(即在hidden.bs.modal事件發(fā)生之前)。
myModal.hide()
手動更新
如果動態(tài)視窗在打開狀態(tài)(比如在出現(xiàn)卷軸的情況下)高度改變,則重新調(diào)整動態(tài)視窗的位置。
myModal.handleUpdate()
銷毀
銷毀一個元素的Modal。(刪除DOM元素上儲存的數(shù)據(jù))
myModal.dispose()
獲取實例
靜態(tài)方法,可以讓你獲取與DOM元素相關(guān)聯(lián)的互動視窗實例。
var myModalEl = document.getElementById('myModal')
var modal = bootstrap.Modal.getInstance(myModalEl) // Returns a Bootstrap modal instance
事件
Bootstrap的model class也有提供一些用于model功能的事件,所有相關(guān)事件都在Model本身被觸發(fā)(在
Event type | Description |
---|---|
show.bs.modal |
當呼叫show實例方法時,此事件會立即觸發(fā)。如果是由點擊事件觸發(fā),被點擊的元素可作為事件的relatedTarget屬性。 |
shown.bs.modal |
當呼動視窗顯示時將會觸發(fā)此事件(等待CSS轉(zhuǎn)換完成)。如果是由點擊事件觸發(fā),被點擊的元素可作為事件的relatedTarget屬性. |
hide.bs.modal |
當呼叫hide實例方法時,將會立即觸發(fā)此事件。 |
hidden.bs.modal |
當互動視窗隱藏后將會觸發(fā)此事件(等CSS轉(zhuǎn)換完成)。 |
hidePrevented.bs.modal |
當顯示互動視窗后將會觸發(fā)此事件,其背景是static的,model以外的點擊或按下esc會被鍵盤選項、data-bs-keyboard設(shè)置為false。 |
var myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', function (event) {
// do something...
})