全面解析Bootstrap彈窗的實(shí)現(xiàn)方法
一. 結(jié)構(gòu)分析
Bootstrap框架中的模態(tài)彈出框,分別運(yùn)用了“modal”、“modal-dialog”和“modal-content”樣式,而彈出窗真正的內(nèi)容都放置在“modal-content”中,其主要又包括三個(gè)部分:
☑ 彈出框頭部,一般使用“modal-header”表示,主要包括標(biāo)題和關(guān)閉按鈕
☑ 彈出框主體,一般使用“modal-body”表示,彈出框的主要內(nèi)容
☑ 彈出框腳部,一般使用“modal-footer”表示,主要放置操作按鈕
<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 -->
二 . data-toggle類觸發(fā)彈窗(無需寫JS)
1 . 模態(tài)彈出窗聲明,只需要自定義兩個(gè)必要的屬性:data-toggle和data-target.
<!-- data-target觸發(fā)模態(tài)彈出窗元素 --> <button class="btn btn-primary" data-toggle="modal" data-target="#mymodal-data" type="button">通過data-target觸發(fā)</button> <!-- 模態(tài)彈出窗內(nèi)容 --> <div class="modal" id="mymodal-data" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true"> <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> </div> </div>
2 . data-參數(shù)說明
除了通過data-toggle和data-target來控制模態(tài)彈出窗之外,Bootstrap框架針對(duì)模態(tài)彈出框還提供了其他自定義data-屬性,來控制模態(tài)彈出窗。
三 . JS觸發(fā)彈窗(要寫JS)
1 . 除了使用自定義屬性觸發(fā)模態(tài)彈出框之外,還可以通過JavaScript方法來觸發(fā)模態(tài)彈出窗。通過給一個(gè)元素一個(gè)事件,來觸發(fā)。比如說給一個(gè)按鈕一個(gè)單擊事件,然后觸發(fā)模態(tài)彈出窗。
HTML:
<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 -->
JS:
$(function(){ $(".btn").click(function(){ $("#mymodal").modal("toggle"); }); });
2 . 使用JavaScript觸發(fā)模態(tài)彈出窗時(shí),Bootstrap框架提供了一些設(shè)置,主要包括
屬性設(shè)置:
$(function(){ $(".btn").click(function(){ $("#mymodal").modal({ keyboard:false }); }); });
參數(shù)設(shè)置:
事件設(shè)置:
$('#myModal').on('hidden.bs.modal', function (e) { // 處理代碼... })
四 . 彈窗尺寸
Bootstrap框架還為模態(tài)彈出窗提供了不同尺寸.
一個(gè)是大尺寸樣式"modal-lg”.
<divclass="modal-dialog modal-lg"> <divclass="modal-content"> ... </div> </div>
另一個(gè)是小尺寸樣式"modal-sm”.
<divclass="modal-dialog modal-sm"> <divclass="modal-content"> ... </div> </div>
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附兩個(gè)精彩的專題:Bootstrap學(xué)習(xí)教程 Bootstrap實(shí)戰(zhàn)教程
以上就是關(guān)于Bootstrap彈窗的實(shí)現(xiàn)方法的全部介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Bootstrap modal 多彈窗之疊加關(guān)閉陰影遮罩問題的解決方法
- Bootstrap modal 多彈窗之疊加引起的滾動(dòng)條遮罩陰影問題
- Bootstrap modal 多彈窗之疊加顯示不出彈窗問題的解決方案
- BootStrap modal模態(tài)彈窗使用小結(jié)
- Bootstrap編寫一個(gè)在當(dāng)前網(wǎng)頁彈出可關(guān)閉的對(duì)話框 非彈窗
- BootStrap的彈出框(Popover)支持鼠標(biāo)移到彈出層上彈窗層不隱藏的原因及解決辦法
- bootstrap實(shí)現(xiàn)彈窗和拖動(dòng)效果
- weebox彈出窗口不居中顯示的解決方法
相關(guān)文章
javascript中call,apply,callee,caller用法實(shí)例分析
這篇文章主要介紹了javascript中call,apply,callee,caller用法,結(jié)合實(shí)例形式分析了javascript中call,apply,callee,caller功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-07-07使用ef6創(chuàng)建oracle數(shù)據(jù)庫的實(shí)體模型遇到的問題及解決方案
這篇文章主要介紹了使用ef6創(chuàng)建oracle數(shù)據(jù)庫的實(shí)體模型遇到的問題及解決方案,需要的朋友可以參考下2017-11-11Javascript實(shí)現(xiàn)鼠標(biāo)移入方向感知
這篇文章主要為大家詳細(xì)介紹了Javascript實(shí)現(xiàn)鼠標(biāo)移入方向感知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Javascript中JSON數(shù)據(jù)分組優(yōu)化實(shí)踐及JS操作JSON總結(jié)
這篇文章主要介紹了Javascript中JSON數(shù)據(jù)分組優(yōu)化實(shí)踐,文中還對(duì)JS操作JSON的要領(lǐng)做了總結(jié),需要的朋友可以參考下2017-12-12js綜合應(yīng)用實(shí)例簡單的表格統(tǒng)計(jì)
在做調(diào)查問卷的過程中,遇到一個(gè)表格的統(tǒng)計(jì)問題,一個(gè)需要用到j(luò)s方面的綜合知識(shí),感覺還不錯(cuò)所以記錄下來與大家分享,感興趣的朋友可以了解下2013-09-09Bootstrap基本插件學(xué)習(xí)筆記之Alert警告框(20)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本插件學(xué)習(xí)筆記之ALert警告框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12