- Bootstrap 教程
- Bootstrap - 首頁
- Bootstrap - 概述
- Bootstrap - 環境搭建
- Bootstrap - 從右到左 (RTL)
- Bootstrap - CSS 變數
- Bootstrap - 色彩模式
- Bootstrap 佈局
- Bootstrap - 斷點
- Bootstrap - 容器
- Bootstrap - 網格系統
- Bootstrap - 列
- Bootstrap - 間距
- Bootstrap - 實用工具類
- Bootstrap - CSS 網格
- Bootstrap 元件
- Bootstrap - 手風琴
- Bootstrap - 警示框
- Bootstrap - 徽章
- Bootstrap - 麵包屑
- Bootstrap - 按鈕
- Bootstrap - 按鈕組
- Bootstrap - 卡片
- Bootstrap - 走馬燈
- Bootstrap - 關閉按鈕
- Bootstrap - 摺疊
- Bootstrap - 下拉選單
- Bootstrap - 列表組
- Bootstrap - 模態框
- Bootstrap - 導航欄
- Bootstrap - 導航 & 標籤頁
- Bootstrap - 側邊欄
- Bootstrap - 分頁
- Bootstrap - 佔位符
- Bootstrap - 氣泡提示框
- Bootstrap - 進度條
- Bootstrap - 滾動監聽
- Bootstrap - 載入動畫
- Bootstrap - 提示框
- Bootstrap - 工具提示
- Bootstrap 表單
- Bootstrap - 表單
- Bootstrap - 表單控制元件
- Bootstrap - 選擇框
- Bootstrap - 複選框 & 單選按鈕
- Bootstrap - 範圍滑塊
- Bootstrap - 輸入組
- Bootstrap - 浮動標籤
- Bootstrap - 佈局
- Bootstrap - 驗證
- Bootstrap 輔助類
- Bootstrap - 清除浮動
- Bootstrap - 顏色 & 背景
- Bootstrap - 彩色連結
- Bootstrap - 聚焦環
- Bootstrap - 圖示連結
- Bootstrap - 定位
- Bootstrap - 寬高比
- Bootstrap - 堆疊
- Bootstrap - 拉伸連結
- Bootstrap - 文字截斷
- Bootstrap - 垂直線
- Bootstrap - 隱藏
- Bootstrap 實用工具類
- Bootstrap - 背景
- Bootstrap - 邊框
- Bootstrap - 顏色
- Bootstrap - 顯示
- Bootstrap - Flexbox
- Bootstrap - 浮動
- Bootstrap - 互動
- Bootstrap - 連結
- Bootstrap - 物件適應
- Bootstrap - 不透明度
- Bootstrap - 溢位
- Bootstrap - 定位
- Bootstrap - 陰影
- Bootstrap - 尺寸
- Bootstrap - 間距
- Bootstrap - 文字
- Bootstrap - 垂直對齊
- Bootstrap - 可見性
- Bootstrap 演示
- Bootstrap - 網格演示
- Bootstrap - 按鈕演示
- Bootstrap - 導航演示
- Bootstrap - 部落格演示
- Bootstrap - 滑塊演示
- Bootstrap - 走馬燈演示
- Bootstrap - 標題演示
- Bootstrap - 頁尾演示
- Bootstrap - 英雄區演示
- Bootstrap - 特色演示
- Bootstrap - 側邊欄演示
- Bootstrap - 下拉選單演示
- Bootstrap - 列表組演示
- Bootstrap - 模態框演示
- Bootstrap - 徽章演示
- Bootstrap - 麵包屑演示
- Bootstrap - Jumbotrons 演示
- Bootstrap-粘性頁尾演示
- Bootstrap-相簿演示
- Bootstrap-登入演示
- Bootstrap-定價演示
- Bootstrap-結賬演示
- Bootstrap-產品演示
- Bootstrap-封面演示
- Bootstrap-儀表盤演示
- Bootstrap-粘性頁尾導航欄演示
- Bootstrap-砌體佈局演示
- Bootstrap-入門模板演示
- Bootstrap-相簿 RTL 演示
- Bootstrap-結賬 RTL 演示
- Bootstrap-走馬燈 RTL 演示
- Bootstrap-部落格 RTL 演示
- Bootstrap-儀表盤 RTL 演示
- Bootstrap 有用資源
- Bootstrap - 常見問題解答
- Bootstrap - 快速指南
- Bootstrap - 有用資源
- Bootstrap - 討論
Bootstrap - 模態框外掛
模態框是一個子視窗,覆蓋在其父視窗之上。通常,其目的是顯示來自獨立來源的內容,以便在不離開父視窗的情況下進行互動。子視窗可以提供資訊、互動或更多功能。
如果您想單獨包含此外掛的功能,則需要 modal.js。否則,如章節 Bootstrap 外掛概述 中所述,您可以包含 bootstrap.js 或壓縮的 bootstrap.min.js。
用法
您可以切換模態框外掛的隱藏內容 -
透過資料屬性 - 在控制器元素(如按鈕或連結)上設定屬性 data-toggle = "modal",以及 data-target = "#identifier" 或 href = "#identifier" 來定位要切換的特定模態框(其 id = "identifier")。
透過 JavaScript - 使用此技術,您可以用一行 JavaScript 呼叫 id = "identifier" 的模態框 -
$('#identifier').modal(options)
示例
以下示例顯示了一個靜態模態框視窗:-
<h2>Example of creating Modals with Twitter Bootstrap</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</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">
This Modal title
</h4>
</div>
<div class = "modal-body">
Add some text here
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
前面程式碼的詳細資訊 -
要呼叫模態框視窗,您需要某種觸發器。您可以使用按鈕或連結。這裡我們使用了按鈕。
如果您檢視上面的程式碼,您會發現在 <button> 標籤中,data-target = "#myModal" 是您想要在頁面上載入的模態框的目標。此程式碼允許您在頁面上建立多個模態框,然後為每個模態框設定不同的觸發器。現在,需要明確的是,您不會同時載入多個模態框,但您可以在頁面上建立多個模態框,以便在不同時間載入。
模態框中有兩個需要注意的類 -
第一個是 .modal,它只是將 <div> 的內容標識為模態框。
第二個是 .fade 類。當模態框切換時,它將導致內容淡入淡出。
aria-labelledby = "myModalLabel",屬性引用模態框標題。
屬性 aria-hidden = "true" 用於在觸發器出現(例如,單擊關聯按鈕)之前保持模態框視窗不可見。
<div class = "modal-header">,modal-header 是定義模態框視窗標題樣式的類。
class = "close",是設定模態框視窗關閉按鈕樣式的 CSS 類 close。
data-dismiss = "modal",是自定義 HTML5 資料屬性。這裡它用於關閉模態框視窗。
class = "modal-body",是 Bootstrap CSS 的 CSS 類,用於設定模態框視窗主體樣式。
class = "modal-footer",是 Bootstrap CSS 的 CSS 類,用於設定模態框視窗頁尾樣式。
data-toggle = "modal",HTML5 自定義資料屬性 data-toggle 用於開啟模態框視窗。
選項
可以透過資料屬性或 JavaScript 傳遞某些選項來自定義模態框視窗的外觀。下表列出了這些選項 -
| 選項名稱 | 型別/預設值 | 資料屬性名稱 | 描述 |
|---|---|---|---|
| backdrop | 布林值或字串 'static' 預設值:true | data-backdrop | 如果不想在使用者單擊模態框外部時關閉模態框,則指定 static 作為背景。 |
| keyboard | 布林值 預設值:true | data-keyboard | 按下 Esc 鍵時關閉模態框;設定為 false 以停用。 |
| show | 布林值 預設值:true | data-show | 初始化時顯示模態框。 |
| remote | 路徑 預設值:false | data-remote | 使用 jQuery 的 .load 方法將內容注入到模態框主體中。如果添加了具有有效 URL 的 href,它將載入該內容。下面顯示了此示例 - <a data-toggle = "modal" href = "remote.html" data-target = "#modal">Click me</a> |
方法
以下是一些可與 modal() 一起使用的方法。
| 方法 | 描述 | 示例 |
|---|---|---|
| 選項 - .modal(options) | 將您的內容啟用為模態框。接受可選的選項物件。 |
$('#identifier').modal({
keyboard: false
})
|
| 切換 - .modal('toggle') | 手動切換模態框。 |
$('#identifier').modal('toggle')
|
| 顯示 - .modal('show') | 手動開啟模態框。 |
$('#identifier').modal('show')
|
| 隱藏 - .modal('hide') | 手動隱藏模態框。 |
$('#identifier').modal('hide')
|
示例
以下示例演示了方法的用法 -
<h2>Example of using methods of Modal Plugin</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</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">
This Modal title
</h4>
</div>
<div class = "modal-body">
Press ESC button to exit.
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
$(function () { $('#myModal').modal({
keyboard: true
})});
</script>
只需點選 Esc 鍵,模態框視窗就會退出。
事件
下表列出了與模態框一起使用的事件。這些事件可用於掛鉤到函式中。
| 事件 | 描述 | 示例 |
|---|---|---|
| show.bs.modal | 在呼叫 show 方法後觸發。 |
$('#identifier').on('show.bs.modal', function () {
// do something…
})
|
| shown.bs.modal | 當模態框已對使用者可見時觸發(將等待 CSS 過渡完成)。 |
$('#identifier').on('shown.bs.modal', function () {
// do something…
})
|
| hide.bs.modal | 當呼叫 hide 例項方法時觸發。 |
$('#identifier').on('hide.bs.modal', function () {
// do something…
})
|
| hidden.bs.modal | 當模態框已完成對使用者的隱藏時觸發。 |
$('#identifier').on('hidden.bs.modal', function () {
// do something…
})
|
示例
以下示例演示了事件的用法 -
<h2>Example of using events of Modal Plugin</h2>
<!-- Button trigger modal -->
<button class = "btn btn-primary btn-lg" data-toggle = "modal" data-target = "#myModal">
Launch demo modal
</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">
This Modal title
</h4>
</div>
<div class = "modal-body">
Click on close button to check Event functionality.
</div>
<div class = "modal-footer">
<button type = "button" class = "btn btn-default" data-dismiss = "modal">
Close
</button>
<button type = "button" class = "btn btn-primary">
Submit changes
</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('Hey, I heard you like modals...');})
});
</script>
如上圖所示,如果您點選“關閉”按鈕,即 隱藏 事件,則會顯示一條警報訊息。