整理自慕課筆記
插件的源文件:modal.js。
在 Bootstrap 框架中把模態(tài)彈出框統(tǒng)一稱為 Modal。這種彈出框效果在大多數(shù) Web 網(wǎng)站的交互中都可見。比如點(diǎn)擊一個(gè)按鈕彈出一個(gè)框,彈出的框可能是一段文件描述,也可能帶有按鈕操作,也有可能彈出的是一張圖片。如下圖所示:

<body><button class="btn btn-primary" type="button">點(diǎn)擊我</button><div class="modal" id="mymodal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">模態(tài)彈出窗標(biāo)題</h4> </div> <div class="modal-body"> <p>模態(tài)彈出窗主體內(nèi)容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary">保存</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --></div><!-- /.modal --><script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script><script src="http://cdn.bootcss.com/bootstrap/2.3.1/js/bootstrap-transition.js"></script><script src="http://cdn.bootcss.com/bootstrap/2.3.1/js/bootstrap-modal.js"></script><script> $(function(){ $(".btn").click(function(){ $("#mymodal").modal("toggle"); }); });</script></body>結(jié)構(gòu)分析
Bootstrap框架中的模態(tài)彈出框,分別運(yùn)用了“modal”、“modal-dialog”和“modal-content”樣式,而彈出窗真正的內(nèi)容都放置在“modal-content”中,其主要又包括三個(gè)部分:
1. 彈出框頭部,一般使用“modal-header”表示,主要包括標(biāo)題和關(guān)閉按鈕
2. 彈出框主體,一般使用“modal-body”表示,彈出框的主要內(nèi)容
3. 彈出框腳部,一般使用“modal-footer”表示,主要放置操作按鈕
<div class="modal show"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">模態(tài)彈出窗標(biāo)題</h4> </div> <div class="modal-body"> <p>模態(tài)彈出窗主體內(nèi)容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary">保存</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --></div><!-- /.modal -->
實(shí)現(xiàn)原理
bootstrap中的“模態(tài)彈出框”有以下幾個(gè)特點(diǎn):
1、模態(tài)彈出窗是固定在瀏覽器中的。
2、在全屏狀態(tài)下,模態(tài)彈出窗寬度是自適應(yīng)的,而且modal-dialog水平居中。
3、當(dāng)瀏覽器視窗大于768px時(shí),模態(tài)彈出窗的寬度為600px。
兩種尺寸選擇
除此之外,Bootstrap框架還為模態(tài)彈出窗提供了不同尺寸,一個(gè)是大尺寸樣式“modal-lg”,另一個(gè)是小尺寸樣式“modal-sm”。其結(jié)構(gòu)上稍做調(diào)整:
<divclass="modal-dialog modal-lg">
觸發(fā)方法
眾所周知,模態(tài)彈出窗在頁(yè)面加載完成時(shí),是被隱藏在頁(yè)面中的,只有通過(guò)一定的動(dòng)作(事件)才能觸發(fā)模態(tài)彈出窗的顯示。在Bootstrap框架中實(shí)現(xiàn)方法有2種,接下來(lái)分別來(lái)介紹這2種觸發(fā)模態(tài)彈出窗的使用方法。
聲明式觸發(fā)方法
方法一
模態(tài)彈出窗聲明,只需要自定義兩個(gè)必要的屬性:data-toggle和data-target(bootstrap中聲明式觸發(fā)方法一般依賴于這些自定義的data-xxx 屬性。比如data-toggle=”” 或者 data-dismiss=”“)。例如:
<!-- 觸發(fā)模態(tài)彈出窗的元素 --><button type="button" data-toggle="modal" data-target="#mymodal" class="btn btn-primary">點(diǎn)擊我會(huì)彈出模態(tài)彈出窗</button><!-- 模態(tài)彈出窗 --><div class="modal fade" id="mymodal"> <div class="modal-dialog"> <div class="modal-content"> <!-- 模態(tài)彈出窗內(nèi)容 --> </div> </div></div>
注意以下事項(xiàng):
1、data-toggle必須設(shè)置為modal(toggle中文翻譯過(guò)來(lái)就是觸發(fā)器);
2、data-target可以設(shè)置為CSS的選擇符,也可以設(shè)置為模態(tài)彈出窗的ID值,一般情況設(shè)置為模態(tài)彈出窗的ID值,因?yàn)镮D值是唯一的值。
方法二
觸發(fā)模態(tài)彈出窗也可以是一個(gè)鏈接元素,那么可以使用鏈接元素自帶的href屬性替代data-target屬性,如:
<!-- 觸發(fā)模態(tài)彈出窗的元素 --><a data-toggle="modal" href="#mymodal" rel="external nofollow" class=" btn btn-primary" >點(diǎn)擊我會(huì)彈出模態(tài)彈出窗</a><!-- 模態(tài)彈出窗 --><div class="modal fade" id="mymodal" > <div class="modal-dialog" > <div class="modal-content" > <!-- 模態(tài)彈出窗內(nèi)容 --> </div> </div></div>
不過(guò)建議還是使用統(tǒng)一使用data-target的方式來(lái)觸發(fā)。
增加過(guò)度動(dòng)畫
為模態(tài)彈出框增加過(guò)度動(dòng)畫效果:
可通過(guò)給“.modal”增加類名“fade”為模態(tài)彈出框增加一個(gè)過(guò)渡動(dòng)畫效果。
<button class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">小的模態(tài)彈出窗</button><div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"> <div class="modal-dialog modal-sm"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">模態(tài)彈出窗標(biāo)題</h4> </div> <div class="modal-body"> <p>模態(tài)彈出窗主體內(nèi)容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div></div>
模態(tài)彈出窗的使用(data-參數(shù)說(shuō)明)
除了通過(guò)data-toggle和data-target來(lái)控制模態(tài)彈出窗之外,Bootstrap框架針對(duì)模態(tài)彈出框還提供了其他自定義data-屬性,來(lái)控制模態(tài)彈出窗。比如說(shuō):是否有灰色背景modal-backdrop,是否可以按ESC鍵關(guān)閉模態(tài)彈出窗。有關(guān)于Modal彈出窗自定義屬性相關(guān)說(shuō)明如下所示:

使用(JavaScript觸發(fā))
除了使用自定義屬性觸發(fā)模態(tài)彈出框之外,還可以通過(guò)JavaScript方法來(lái)觸發(fā)模態(tài)彈出窗。通過(guò)給一個(gè)元素一個(gè)事件,來(lái)觸發(fā)。比如說(shuō)給一個(gè)按鈕一個(gè)單擊事件,然后觸發(fā)模態(tài)彈出窗。如下面的一個(gè)簡(jiǎn)單示例:
<!-- 觸發(fā)模態(tài)彈出窗元素 --><button class="btn btn-primary" type="button">點(diǎn)擊我</button><!-- 模態(tài)彈出窗內(nèi)容 --><div class="modal" id="mymodal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title">模態(tài)彈出窗標(biāo)題</h4> </div> <div class="modal-body"> <p>模態(tài)彈出窗主體內(nèi)容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關(guān)閉</button> <button type="button" class="btn btn-primary">保存</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --></div><!-- /.modal -->
JavaScript觸發(fā)的彈出窗代碼:
$(function(){ $(".btn").click(function(){ $("#mymodal").modal(); });});設(shè)置參數(shù)
使用JavaScript觸發(fā)模態(tài)彈出窗時(shí),Bootstrap框架提供了一些設(shè)置,主要包括屬性設(shè)置、參數(shù)設(shè)置和事件設(shè)置。
屬性設(shè)置
模態(tài)彈出窗默認(rèn)支持的自定義屬性主要有:

比如你不想讓用戶按ESC鍵關(guān)閉模態(tài)彈出窗,你就可以這樣做:
$(function(){ $(".btn").click(function(){ $("#mymodal").modal({ keyboard:false }); });});參數(shù)設(shè)置
在Bootstrap框架中還為模態(tài)彈出窗提供了三種參數(shù)設(shè)置,具體說(shuō)明如下:

事件設(shè)置
模態(tài)彈出窗還支持四種類型的事件,分別是模態(tài)彈出窗的彈出前、彈出后,關(guān)閉前、關(guān)閉后,具體描述如下:

調(diào)用方法也非常簡(jiǎn)單:
$('#myModal').on('hidden.bs.modal', function (e) { // 處理代碼...})以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注