bootstrap中的模態框(modal),不一樣於Tooltips,模態框以彈出對話框的形式出現,具備最小和最實用的功能集。javascript
務必將模態框的 HTML 代碼放在文檔的最高層級內(也就是說,儘可能做爲 body 標籤的直接子元素),以免其餘組件影響模態框的展示或功能。css

默認的modal示例:html
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Bootstrap Modal</title>
- <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
- </head>
- <body>
- <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
- Launch demo modal
- </button>
-
- <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close">
- <span aria-hidden="true">×</span>
- </button>
- <h4 class="modal-title" id="myModalLabel">Modal title</h4>
- </div>
- <div class="modal-body">
- <p>One fine body…</p>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
- <button type="button" class="btn btn-primary">Save</button>
- </div>
- </div>
- </div>
- </div>
-
- <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
- <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
- </body>
- </html>
- 爲 .modal 添加 role="dialog",用於指定模態框爲對話框。
- 爲 .modal-dialog 添加 aria-hidden="true" 屬性。
- 經過 aria-describedby 屬性爲模態框 .modal 添加描述性信息。
關閉動畫java
若是你不須要模態框彈出時的動畫效果(淡入淡出效果),刪掉 .fade 類便可。jquery
經過按鈕屬性顯示不一樣內容
當有一堆按鈕,都要觸發相同的模態框(如:向好友列表中某我的發消息),只是有用戶ID不一樣,那麼可使用data-whatever配合event.relatedtarget來實現:ajax
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>Bootstrap Modal</title>
- <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
- </head>
- <body>
-
-
- <div class="panel panel-default">
- <div class="panel-heading">好友列表</div>
- <div class="panel-body">
- <div class="list-group" role="group" aria-label="好友列表">
- <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
- data-whatever="張三">張三
- </button>
- <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
- data-whatever="李四">李四
- </button>
- <button type="button" class="list-group-item" data-toggle="modal" data-target="#exampleModal"
- data-whatever="王二">王二
- </button>
- </div>
- </div>
- </div>
-
- <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
- <div class="modal-dialog" role="document">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
- aria-hidden="true">×</span></button>
- <h4 class="modal-title" id="exampleModalLabel">New message</h4>
- </div>
- <div class="modal-body">
- <form>
- <div class="form-group">
- <label for="recipient-name" class="control-label">Recipient:</label>
- <input type="text" class="form-control" id="recipient-name">
- </div>
- <div class="form-group">
- <label for="message-text" class="control-label">Message:</label>
- <textarea class="form-control" id="message-text"></textarea>
- </div>
- </form>
- </div>
- <div class="modal-footer">
- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
- <button type="button" class="btn btn-primary">Send message</button>
- </div>
- </div>
- </div>
- </div>
- <script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
- <script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
- <script>
- $('#exampleModal').on('show.bs.modal', function (event) {
- var button = $(event.relatedTarget) // 觸發事件的按鈕
- var recipient = button.data('whatever') // 解析出data-whatever內容
- var modal = $(this)
- modal.find('.modal-title').text('Message To ' + recipient)
- modal.find('.modal-body input').val(recipient)
- })
- </script>
- </body>
- </html>
一般不需寫 JavaScript 代碼也可激活模態框。經過在一個起控制器做用的元素(例如:按鈕)上添加 data-toggle="modal" 屬性,或者 data-target="#foo" 屬性,再或者 href="#foo" 屬性,用於指向被控制的模態框。
bootstrap
經過JavaScript調用modalide
一般只需一行 javascript 代碼,便可經過元素的 id myModal 調用模態框:函數
- $('#myModal').modal(options)
JavaScript參數:動畫
能夠將選項經過 data 屬性或 JavaScript 代碼傳遞。對於 data 屬性,須要將參數名稱放到 data- 以後,例如 data-backdrop=""。
名稱 |
類型 |
默認值 |
描述 |
backdrop |
boolean或'static' |
true |
默認狀況下,點擊其它位置,模態框自動關閉,你能夠設置此屬性爲false或者"static"防止模態框自動關閉 |
keyboard |
boolean |
true |
鍵盤上的 esc 鍵被按下時關閉模態框。 |
show |
path |
true |
模態框初始化以後就當即顯示出來。 |
remote |
path |
false |
這個參數在v4裏會去掉(能夠本身寫ajax請求來實現)若是提供的是 URL,將利用 jQuery 的 load 方法今後 URL 地址加載要展現的內容(只加載一次)並插入 .modal-content 內。若是使用的是 data 屬性 API,還能夠利用 href 屬性指定內容來源地址。下面是一個實例: <a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a> |
調用示例
1.將頁面中的某塊內容做爲模態框激活。接受可選參數 object。
- $('#myModal').modal({
- keyboard: false
- })
2.手動打開或關閉模態框。在模態框顯示或隱藏以前返回到主調函數中(也就是,在觸發 shown.bs.modal 或 hidden.bs.modal 事件以前)。
- $('#myModal').modal('toggle')
3.手動打開模態框。在模態框顯示以前返回到主調函數中 (也就是,在觸發 shown.bs.modal 事件以前)。
- $('#myModal').modal('show')
4.手動隱藏模態框。在模態框隱藏以前返回到主調函數中 (也就是,在觸發 hidden.bs.modal 事件以前)。
- $('#myModal').modal('hide')
5.更新模態框,在模態框動態添加或刪除內容時:
- $('#myModal').modal('handleUpdate')
綁定事件
Bootstrap 的模態框類提供了一些事件用於監聽並執行你本身的代碼。
事件類型 |
描述 |
show.bs.modal |
show 方法調用以後當即觸發該事件。若是是經過點擊某個做爲觸發器的元素,則此元素能夠經過事件的 relatedTarget 屬性進行訪問。 |
shown.bs.modal |
此事件在模態框已經顯示出來(而且同時在 CSS 過渡效果完成)以後被觸發。若是是經過點擊某個做爲觸發器的元素,則此元素能夠經過事件的 relatedTarget 屬性進行訪問。 |
hide.bs.modal |
hide 方法調用以後當即觸發該事件。 |
hidden.bs.modal |
此事件在模態框被隱藏(而且同時在 CSS 過渡效果完成)以後被觸發。 |
loaded.bs.modal |
從遠端的數據源加載完數據以後觸發該事件。 |
如:
- $('#myModal').on('hidden.bs.modal', function (e) {
- // do something...
- })