轉載: https://www.w3h5.com/post/74.html
模態框(Modal)通俗的說就是在父窗體上彈出的一個子窗體。
通常用來顯示一個單獨的源內容或者是對一些模塊進行進一步詳細的介紹,可以在不離開父窗體的情況下進行一些互動和內容的交互。
如果只是單獨引用該插件的功能,只需要引用 modal.js ,也可以引用 bootstrap.js 或壓縮版的 bootstrap.min.js。
一、用法:
切換模態框(Modal)插件的隱藏內容:
- 通過 data 屬性:在控制器元素(比如按鈕或者鏈接)上添加屬性 data-toggle="modal",同時設置 data-target="#identifier" 或 href="#identifier" 來指定要切換的特定的模態框(id 為 identifier的)。
- 通過 JavaScript:使用這種技術,您可以通過簡單的一行 JavaScript 來調用帶有 id="identifier" 的模態框:
$('#identifier').modal(options)
實例:
注意:因為 Bootstrap.js 需要依賴 jquery 所以需要引入 jquery.js 文件
測試代碼如下:
<div class="container"> <h2>創建模態框(Modal)</h2> <!-- 按鈕觸發模態框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">開始演示模態框</button> <!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">模態框(Modal)標題</h4> </div> <div class="modal-body">在這里添加一些文本</div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> <button type="button" class="btn btn-primary">提交更改</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal --> </div> </div>
測試結果如下圖:

代碼講解:
使用模態窗口,需要有某種觸發器,可以使用按鈕或鏈接,這里我們使用的是按鈕。
仔細查看上面的代碼,會發現在 <button> 標簽中,data-target="#myModal" 是要在頁面上加載的模態框的目標。
可以在頁面上創建多個模態框(模態框的內容在頁面中定義),然后為每個模態框創建不同的觸發器。
不能在同一時間加載多個模塊,但可以在頁面上創建多個在不同時間進行加載。
在模態框中需要注意兩點:
第一是 .modal,用來把 <div> 的內容識別為模態框。
第二是 .fade,當模態框被切換時,它會引起內容淡入淡出。
- aria-labelledby="myModalLabel",該屬性引用模態框的標題。
- 屬性 aria-hidden="true" 用於保持模態窗口不可見,直到觸發器被觸發為止(比如點擊在相關的按鈕上)。
- <div class="modal-header">,modal-header 是為模態窗口的頭部定義樣式的類。
- class="close",close 是一個 CSS class,用於為模態窗口的關閉按鈕設置樣式。
- data-dismiss="modal",是一個自定義的 HTML5 data 屬性。在這里它被用於關閉模態窗口。
- class="modal-body",是 Bootstrap CSS 的一個 CSS class,用於為模態窗口的主體設置樣式。
- class="modal-footer",是 Bootstrap CSS 的一個 CSS class,用於為模態窗口的底部設置樣式。
- data-toggle="modal",HTML5 自定義的 data 屬性 data-toggle 用於打開模態窗口。
二、選項
有一些選項可以用來定制模態窗口(Modal Window)的外觀和感觀,可以將選項通過 data 屬性或 JavaScript 來傳遞,對於 data 屬性,需要將參數名稱放到 data- 之后,例如 data-backdrop=""。
1、backdrop 屬性有 boolean 或 string 'static',默認值為 true, data-backdrop 是指定一個靜態的背景,當用戶點擊模態框外部時不會關閉模態框。
不過 data-backdrop 有時候會與頁面沖突,如果要關閉背景,將其設置為 false 即可: data-backdrop="false" 。
2、keyboard 屬性有 boolean,默認值:true , data-keyboard 是當按下 esc 鍵時關閉模態框,設置為 false 時則按鍵無效。
3、show 屬性有 boolean ,默認值:true,data-show 是當初始化時顯示模態框。
4、remote 類型是 path ,默認值:false ,data-remote 使用 jQuery .load 方法,為模態框的主體注入內容。如果添加了一個帶有有效 URL 的 href,則會加載其中的內容。
如下面的實例所示:
<a data-toggle="modal" href="remote.html" data-target="#modal">請點擊我</a>
三、方法
下面是一些可與 modal() 一起使用的方法。
1、Options: .modal(options) 把內容作為模態框激活,接受一個可選的選項對象。
$('#identifier').modal({ keyboard: false })
2、Toggle: .modal('toggle') 手動切換模態框。
$('#identifier').modal('toggle')
3、Show: .modal('show') 手動打開模態框。
$('#identifier').modal('show')
4、Hide: .modal('hide') 手動隱藏模態框。
$('#identifier').modal('hide')
實例:
下面的演示方法的用法:
<!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">模態框(Modal)標題</h4> </div> <div class="modal-body">按下 ESC 按鈕退出。</div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> <button type="button" class="btn btn-primary">提交更改</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div> <!-- /.modal --> <script> $(function() { $('#myModal').modal({ keyboard: true }) }); </script>
運行結果如下:按 ESC 或者點擊屏幕,模態框會關閉。

四、事件
下面試模態框中用到的事件,這些事件可在函數中當鈎子使用。
1、show.bs.modal 在調用 show 方法后觸發。
$('#identifier').on('show.bs.modal', function () { // 執行一些動作... })
2、shown.bs.modal 當模態框對用戶可見時觸發(將等待 CSS 過渡效果完成)。
$('#identifier').on('shown.bs.modal', function () { // 執行一些動作... })
3、hide.bs.modal 當調用 hide 實例方法時觸發。
$('#identifier').on('hide.bs.modal', function () { // 執行一些動作... })
4、hidden.bs.modal 當模態框完全對用戶隱藏時觸發。
$('#identifier').on('hidden.bs.modal', function () { // 執行一些動作... })
實例:
<!-- 模態框(Modal) --> <h2>模態框(Modal)插件事件</h2> <!-- 按鈕觸發模態框 --> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">開始演示模態框</button> <!-- 模態框(Modal) --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title" id="myModalLabel">模態框(Modal)標題</h4> </div> <div class="modal-body">點擊關閉按鈕檢查事件功能。</div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> <button type="button" class="btn btn-primary">提交更改</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div> <!-- /.modal --> <script> $(function() { $('#myModal').modal('hide') }); </script> <script> $(function() { $('#myModal').on('hide.bs.modal', function() { alert('嘿,我聽說您喜歡模態框...'); }) }); </script>
運行結果如下:點擊了 關閉 按鈕,即 hide 事件,則會顯示一個警告消息。
