?
本文檔使用 PHP中文網手冊 發(fā)布
使用 Bootstrap 的 JavaScript 模式插件為您的網站添加對話框,了解 Lightbox,用戶通知或完全自定義內容。
在開始使用 Bootstrap 的模塊組件之前,請務必閱讀以下內容,因為我們的菜單選項最近已更改。
Modals 是用 HTML,CSS 和 JavaScript 構建的。它們位于文檔中的所有其他位置,并從滾動中移除,<body>
以便模式內容可以滾動。
點擊模式“背景”會自動關閉模式。
Bootstrap 一次只支持一個模式窗口。不支持嵌套模式,因為我們認為它們是糟糕的用戶體驗。
模態(tài)的使用position: fixed
,有時候對它的渲染有點特別。盡可能將模態(tài) HTML 置于頂層位置,以避免其他元素的潛在干擾。嵌套.modal
在另一個固定元素中時,您可能會遇到問題。
由于position: fixed
這一點,在移動設備上使用模塊還有一些注意事項。有關詳情,請參閱我們的瀏覽器支持文檔
由于 HTML5 定義了其語義,因此autofocus
HTML 屬性在 Bootstrap 模式中不起作用。要達到相同的效果,請使用一些自定義 JavaScript:
$('#myModal').on('shown.bs.modal', function () { $('#myInput').trigger('focus')})
請繼續(xù)閱讀演示和使用指南。
下面是一個靜態(tài)模式的例子(指其position
與display
被覆蓋)。包括模態(tài)標題,模態(tài)主體(必需padding
)和模態(tài)頁腳(可選)。我們要求您在可能的情況下包含解除操作的模態(tài)標頭,或者提供另一個明確的解雇操作。
<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>
點擊下面的按鈕來切換工作模式演示。它會滑下并從頁面頂部淡入。
<!-- 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>
當模式對用戶的視口或設備變得太長時,它們將獨立于頁面自身進行滾動。試試下面的演示,看看我們的意思。
<!-- 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>
工具提示和彈出可以根據需要放置在模塊中。當模式關閉時,其中的任何工具提示和彈出窗口也會自動解除。
<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
利用模式中的引導網格系統(tǒ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>
有一堆按鈕都會觸發(fā)相同的模式,但內容稍有不同?使用event.relatedTarget
和 HTML data-*
屬性(可能通過 jQuery)根據點擊哪個按鈕來改變模式的內容。
下面是一個現(xiàn)場演示,后面是示例 HTML 和 JavaScript。有關更多信息,請閱讀模式事件文檔以獲取詳細信息在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)})
對于簡單出現(xiàn)而不是淡入查看的模態(tài),從模態(tài)標記中刪除.fade
類。
<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..." aria-hidden="true"> ...</div>
如果模態(tài)的高度在打開時發(fā)生變化,您應該調用$('#myModal').data('bs.modal').handleUpdate()
或$('#myModal').modal('handleUpdate')
重新調整模態(tài)的位置以防出現(xiàn)滾動條。
一定要加role="dialog"
和aria-labelledby="..."
,引用模式稱號,.modal
以及role="document"
對.modal-dialog
本身。另外,你可以在上.modal
用aria-describedby
來描述你的模態(tài)對話框。
以模式嵌入 YouTube 視頻需要不在 Bootstrap 中的其他 JavaScript 才能自動停止播放等等。看到這個有用的堆棧溢出帖子了解更多信息。
Modals 有兩個可選的尺寸,可通過修飾符類放置在一個.modal-dialog
上。這些尺寸在某些斷點處啟動以避免較窄視口上的水平滾動條。
<!-- 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ù)據屬性或JavaScript來按需切換隱藏內容。它還增加.modal-open
了<body>
覆蓋默認的滾動行為,并生成一個.modal-backdrop
提供點擊區(qū)域來解除在模態(tài)外單擊時顯示的模態(tài)。
無需編寫 JavaScript 即可激活模式。在一個控制器元素上設置data-toggle="modal"
,就像一個按鈕一起,data-target="#foo"
或者href="#foo"
將一個特定的模式作為切換目標。
<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
用一行 JavaScript 調用帶 idmyModal
的模式:
$('#myModal').modal(options)
選項可以通過數(shù)據屬性或 JavaScript 傳遞。對于數(shù)據屬性,請將選項名稱附加到data-
中,如data-backdrop=""
。
名稱 | 類型 | 默認 | 描述 |
---|---|---|---|
backdrop | 布爾值或字符串'static' | 真 | 包含模態(tài)背景元素?;蛘?,為點擊時不關閉模式的背景指定靜態(tài)。 |
keyboard | 布爾 | 真 | 按下退出鍵時關閉模式 |
focus | 布爾 | 真 | 初始化時將重點放在模態(tài)上。 |
show | 布爾 | 真 | 初始化時顯示模式。 |
所有 API 方法都是異步的并開始轉換。一旦轉換開始但在結束之前,它們就立即返回給調用者。另外,對轉換組件的方法調用將被忽略。
查看我們的 JavaScript 文檔以獲取更多信息。
.modal(options)
激活您的內容作為模式。接受可選的選項object
。
$('#myModal').modal({ keyboard: false})
.modal('toggle')
手動切換模式。返回到之前的模態(tài)主叫方實際上已顯示或隱藏(IE 瀏覽器之前shown.bs.modal
或hidden.bs.modal
事件發(fā)生時)。
$('#myModal').modal('toggle')
.modal('show')
手動打開模式。在實際顯示模態(tài)之前(即shown.bs.modal
事件發(fā)生之前)返回給調用者。
$('#myModal').modal('show')
.modal('hide')
手動隱藏模式。在模態(tài)被隱藏之前(即hidden.bs.modal
事件發(fā)生之前)返回給調用者。
$('#myModal').modal('hide')
.modal('handleUpdate')
如果模態(tài)的高度在打開時發(fā)生變化(例如出現(xiàn)滾動條),則手動重新調整模態(tài)的位置。
$('#myModal').modal('handleUpdate')
.modal('dispose')
銷毀一個元素的模態(tài)。
Bootstrap 的模態(tài)類公開了一些鉤住模態(tài)功能的事件。所有的模態(tài)事件都是在模態(tài)本身(即在<div class="modal">
)處被觸發(fā)的。
事件類型 | 描述 |
---|---|
show.bs.modal | 當 show instance 方法被調用時,此事件立即觸發(fā)。如果由于點擊而導致點擊的元素可用作事件的 relatedTarget 屬性。 |
shown.bs.modal | 當模式對用戶可見時會觸發(fā)此事件(將等待 CSS 轉換完成)。如果由于點擊而導致點擊的元素可用作事件的 relatedTarget 屬性。 |
hide.bs.modal | 當調用隱藏實例方法時立即觸發(fā)此事件。 |
hidden.bs.modal | 當模式完成對用戶的隱藏時,會觸發(fā)此事件(等待 CSS 轉換完成)。 |
$('#myModal').on('hidden.bs.modal', function (e) { // do something...})