?
This document uses PHP Chinese website manual Release
使用 Bootstrap 的 JavaScript 模式插件為您的網(wǎng)站添加對(duì)話框,了解 Lightbox,用戶通知或完全自定義內(nèi)容。
在開始使用 Bootstrap 的模塊組件之前,請(qǐng)務(wù)必閱讀以下內(nèi)容,因?yàn)槲覀兊牟藛芜x項(xiàng)最近已更改。
Modals 是用 HTML,CSS 和 JavaScript 構(gòu)建的。它們位于文檔中的所有其他位置,并從滾動(dòng)中移除,<body>
以便模式內(nèi)容可以滾動(dòng)。
點(diǎn)擊模式“背景”會(huì)自動(dòng)關(guān)閉模式。
Bootstrap 一次只支持一個(gè)模式窗口。不支持嵌套模式,因?yàn)槲覀冋J(rèn)為它們是糟糕的用戶體驗(yàn)。
模態(tài)的使用position: fixed
,有時(shí)候?qū)λ匿秩居悬c(diǎn)特別。盡可能將模態(tài) HTML 置于頂層位置,以避免其他元素的潛在干擾。嵌套.modal
在另一個(gè)固定元素中時(shí),您可能會(huì)遇到問題。
由于position: fixed
這一點(diǎn),在移動(dòng)設(shè)備上使用模塊還有一些注意事項(xiàng)。有關(guān)詳情,請(qǐng)參閱我們的瀏覽器支持文檔
由于 HTML5 定義了其語(yǔ)義,因此autofocus
HTML 屬性在 Bootstrap 模式中不起作用。要達(dá)到相同的效果,請(qǐng)使用一些自定義 JavaScript:
$('#myModal').on('shown.bs.modal', function () { $('#myInput').trigger('focus')})
請(qǐng)繼續(xù)閱讀演示和使用指南。
下面是一個(gè)靜態(tài)模式的例子(指其position
與display
被覆蓋)。包括模態(tài)標(biāo)題,模態(tài)主體(必需padding
)和模態(tài)頁(yè)腳(可選)。我們要求您在可能的情況下包含解除操作的模態(tài)標(biāo)頭,或者提供另一個(gè)明確的解雇操作。
<div class="modal" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p>Modal body text goes here.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary">Save changes</button> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> </div> </div></div>
點(diǎn)擊下面的按鈕來(lái)切換工作模式演示。它會(huì)滑下并從頁(yè)面頂部淡入。
<!-- Button trigger modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Launch demo modal</button><!-- Modal --><div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div></div>
當(dāng)模式對(duì)用戶的視口或設(shè)備變得太長(zhǎng)時(shí),它們將獨(dú)立于頁(yè)面自身進(jìn)行滾動(dòng)。試試下面的演示,看看我們的意思。
<!-- Button trigger modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong"> Launch demo modal</button><!-- Modal --><div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div></div>
工具提示和彈出可以根據(jù)需要放置在模塊中。當(dāng)模式關(guān)閉時(shí),其中的任何工具提示和彈出窗口也會(huì)自動(dòng)解除。
<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-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>
通過.modal-body
嵌套.container-fluid
利用模式中的引導(dǎo)網(wǎng)格系統(tǒng)內(nèi)。然后,像任何其他地方一樣使用正常的網(wǎng)格系統(tǒng)類。
<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 ml-auto">.col-md-4 .ml-auto</div> </div> <div class="row"> <div class="col-md-3 ml-auto">.col-md-3 .ml-auto</div> <div class="col-md-2 ml-auto">.col-md-2 .ml-auto</div> </div> <div class="row"> <div class="col-md-6 ml-auto">.col-md-6 .ml-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>
有一堆按鈕都會(huì)觸發(fā)相同的模式,但內(nèi)容稍有不同?使用event.relatedTarget
和 HTML data-*
屬性(可能通過 jQuery)根據(jù)點(diǎn)擊哪個(gè)按鈕來(lái)改變模式的內(nèi)容。
下面是一個(gè)現(xiàn)場(chǎng)演示,后面是示例 HTML 和 JavaScript。有關(guān)更多信息,請(qǐng)閱讀模式事件文檔以獲取詳細(xì)信息在relatedTarget
上。
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button><div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">New message</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <form> <div class="form-group"> <label for="recipient-name" class="col-form-label">Recipient:</label> <input type="text" class="form-control" id="recipient-name"> </div> <div class="form-group"> <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-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Send message</button> </div> </div> </div></div>
$('#exampleModal').on('show.bs.modal', function (event) { var button = $(event.relatedTarget) // Button that triggered the modal var recipient = button.data('whatever') // Extract info from data-* attributes // If necessary, you could initiate an AJAX request here (and then do the updating in a callback). // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead. var modal = $(this) modal.find('.modal-title').text('New message to ' + recipient) modal.find('.modal-body input').val(recipient)})
對(duì)于簡(jiǎn)單出現(xiàn)而不是淡入查看的模態(tài),從模態(tài)標(biāo)記中刪除.fade
類。
<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..." aria-hidden="true"> ...</div>
如果模態(tài)的高度在打開時(shí)發(fā)生變化,您應(yīng)該調(diào)用$('#myModal').data('bs.modal').handleUpdate()
或$('#myModal').modal('handleUpdate')
重新調(diào)整模態(tài)的位置以防出現(xiàn)滾動(dòng)條。
一定要加role="dialog"
和aria-labelledby="..."
,引用模式稱號(hào),.modal
以及role="document"
對(duì).modal-dialog
本身。另外,你可以在上.modal
用aria-describedby
來(lái)描述你的模態(tài)對(duì)話框。
以模式嵌入 YouTube 視頻需要不在 Bootstrap 中的其他 JavaScript 才能自動(dòng)停止播放等等??吹竭@個(gè)有用的堆棧溢出帖子了解更多信息。
Modals 有兩個(gè)可選的尺寸,可通過修飾符類放置在一個(gè).modal-dialog
上。這些尺寸在某些斷點(diǎn)處啟動(dòng)以避免較窄視口上的水平滾動(dòng)條。
<!-- Large modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">Large modal</button><div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg"> <div class="modal-content"> ... </div> </div></div><!-- Small modal --><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-sm">Small modal</button><div class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"> <div class="modal-dialog modal-sm"> <div class="modal-content"> ... </div> </div></div>
模式插件通過數(shù)據(jù)屬性或JavaScript來(lái)按需切換隱藏內(nèi)容。它還增加.modal-open
了<body>
覆蓋默認(rèn)的滾動(dòng)行為,并生成一個(gè).modal-backdrop
提供點(diǎn)擊區(qū)域來(lái)解除在模態(tài)外單擊時(shí)顯示的模態(tài)。
無(wú)需編寫 JavaScript 即可激活模式。在一個(gè)控制器元素上設(shè)置data-toggle="modal"
,就像一個(gè)按鈕一起,data-target="#foo"
或者href="#foo"
將一個(gè)特定的模式作為切換目標(biāo)。
<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
用一行 JavaScript 調(diào)用帶 idmyModal
的模式:
$('#myModal').modal(options)
選項(xiàng)可以通過數(shù)據(jù)屬性或 JavaScript 傳遞。對(duì)于數(shù)據(jù)屬性,請(qǐng)將選項(xiàng)名稱附加到data-
中,如data-backdrop=""
。
名稱 | 類型 | 默認(rèn) | 描述 |
---|---|---|---|
backdrop | 布爾值或字符串'static' | 真 | 包含模態(tài)背景元素。或者,為點(diǎn)擊時(shí)不關(guān)閉模式的背景指定靜態(tài)。 |
keyboard | 布爾 | 真 | 按下退出鍵時(shí)關(guān)閉模式 |
focus | 布爾 | 真 | 初始化時(shí)將重點(diǎn)放在模態(tài)上。 |
show | 布爾 | 真 | 初始化時(shí)顯示模式。 |
所有 API 方法都是異步的并開始轉(zhuǎn)換。一旦轉(zhuǎn)換開始但在結(jié)束之前,它們就立即返回給調(diào)用者。另外,對(duì)轉(zhuǎn)換組件的方法調(diào)用將被忽略。
查看我們的 JavaScript 文檔以獲取更多信息。
.modal(options)
激活您的內(nèi)容作為模式。接受可選的選項(xiàng)object
。
$('#myModal').modal({ keyboard: false})
.modal('toggle')
手動(dòng)切換模式。返回到之前的模態(tài)主叫方實(shí)際上已顯示或隱藏(IE 瀏覽器之前shown.bs.modal
或hidden.bs.modal
事件發(fā)生時(shí))。
$('#myModal').modal('toggle')
.modal('show')
手動(dòng)打開模式。在實(shí)際顯示模態(tài)之前(即shown.bs.modal
事件發(fā)生之前)返回給調(diào)用者。
$('#myModal').modal('show')
.modal('hide')
手動(dòng)隱藏模式。在模態(tài)被隱藏之前(即hidden.bs.modal
事件發(fā)生之前)返回給調(diào)用者。
$('#myModal').modal('hide')
.modal('handleUpdate')
如果模態(tài)的高度在打開時(shí)發(fā)生變化(例如出現(xiàn)滾動(dòng)條),則手動(dòng)重新調(diào)整模態(tài)的位置。
$('#myModal').modal('handleUpdate')
.modal('dispose')
銷毀一個(gè)元素的模態(tài)。
Bootstrap 的模態(tài)類公開了一些鉤住模態(tài)功能的事件。所有的模態(tài)事件都是在模態(tài)本身(即在<div class="modal">
)處被觸發(fā)的。
事件類型 | 描述 |
---|---|
show.bs.modal | 當(dāng) show instance 方法被調(diào)用時(shí),此事件立即觸發(fā)。如果由于點(diǎn)擊而導(dǎo)致點(diǎn)擊的元素可用作事件的 relatedTarget 屬性。 |
shown.bs.modal | 當(dāng)模式對(duì)用戶可見時(shí)會(huì)觸發(fā)此事件(將等待 CSS 轉(zhuǎn)換完成)。如果由于點(diǎn)擊而導(dǎo)致點(diǎn)擊的元素可用作事件的 relatedTarget 屬性。 |
hide.bs.modal | 當(dāng)調(diào)用隱藏實(shí)例方法時(shí)立即觸發(fā)此事件。 |
hidden.bs.modal | 當(dāng)模式完成對(duì)用戶的隱藏時(shí),會(huì)觸發(fā)此事件(等待 CSS 轉(zhuǎn)換完成)。 |
$('#myModal').on('hidden.bs.modal', function (e) { // do something...})