BootStrap的兩種模態(tài)框方式
bootstrap的彈出層
bootstrap彈出層有多種觸發(fā)方式,以下是我用到的幾種方式:
1.方法一:button中屬性觸發(fā)
注意:button中的data-target內(nèi)容應(yīng)該和要和彈出層中的id保持一致
data-target=”#mymodal-data”——– id=”mymodal-data” <!--在button上綁定觸發(fā)彈出層的屬性--> <button class="btn btn-primary delete" data-toggle="modal" data-target="#mymodal-data" data-whatever="@mdo"> 修改 </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">彈出層標(biāo)題</h4> </div> <div class="modal-body"> <p>彈出層主體內(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>
結(jié)果:
2.方法二:通過js綁定
注意:將button的id和彈出層的id分別賦給 $m_btn和$modal,當(dāng)$m_btn被點(diǎn)擊后$modal彈出。
<button class="btn btn-info" type="button" id="y-modalBtnAdd" > <label >添加</label></button> <!-- 模態(tài)彈出窗內(nèi)容 --> <div class="modal" id="y-myModalAdd" 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">彈出層標(biāo)題</h4> </div> <div class="modal-body"> <p>通過js綁定button和彈出層觸發(fā)</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> <!--js代碼--> <script type="text/javascript"> $(function(){ // dom加載完畢 var $m_btn = $('#y-modalBtnAdd'); //y-modalBtnAdd是button的id var $modal = $('#y-myModalAdd'); //y-myModalAdd是彈出的遮罩層的id,通過這兩個(gè)id進(jìn)行綁定 $m_btn.on('click', function(){ $modal.modal({backdrop: 'static'}); }); }); </script>
結(jié)果:
3.方法三:點(diǎn)擊表格一行,彈出彈出層
動(dòng)態(tài)給tr標(biāo)簽加彈出的觸發(fā)屬性
<!--表格--> <table class="table table-bordered " style="width: 400px"> <thead> <tr> <th>一</th> <th>二</th> <th>三</th> </tr> </thead> <tbody class="tableBody"> <tr> <td>one</td> <td>two</td> <td>three</td> </tr> <tr> <td>four</td> <td>five</td> <td>six</td> </tr> </tbody> </table> <!-- 模態(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">彈出層標(biāo)題</h4> </div> <div class="modal-body"> <p>點(diǎn)擊表格一行內(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> <!--js代碼--> <script type="text/javascript"> $(function () { $(".tableBody>tr").each(function () { $(this).on("click",function () { $(this).attr({"data-toggle":"modal","data-target":"#mymodal-data","data-whatever":"@mdo"}); }) }); }); </script>
結(jié)果:
bootstrap的彈出層在整個(gè)屏幕的上半部分,可以將它居中顯示。(方法二可以讓彈出層居中顯示)
$(function(){ // dom加載完畢 var $m_btn = $('#y-modalBtnAdd'); y-modalBtnAdd是button的id var $modal = $('#y-myModalAdd'); y-myModalAdd是彈出的遮罩層的id,通過這兩個(gè)id進(jìn)行綁定 // 測(cè)試 bootstrap 居中 ,bootstrap的彈出層默認(rèn)是左右居中,上下則是偏上,此代碼將彈出層上下也居中了,但是會(huì)抖 動(dòng)一下 $modal.on('shown.bs.modal', function(){ var $this = $(this); var $modal_dialog = $this.find('.modal-dialog'); var m_top = ( $(document).height() - $modal_dialog.height() )/2; $modal_dialog.css({'margin': m_top + 'px auto'}); }); }); </script>
以上所述是小編給大家介紹的BootStrap的兩種模態(tài)框方式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 淺析BootStrap中Modal(模態(tài)框)使用心得
- bootstrap模態(tài)框消失問題的解決方法
- Bootstrap模態(tài)框調(diào)用功能實(shí)現(xiàn)方法
- Bootstrap模態(tài)框(modal)垂直居中的實(shí)例代碼
- BootStrap日期控件在模態(tài)框中選擇時(shí)間下拉菜單無效的原因及解決辦法(火狐下不能點(diǎn)擊)
- 淺析BootStrap模態(tài)框的使用(經(jīng)典)
- BOOTSTRAP時(shí)間控件顯示在模態(tài)框下面的bug修復(fù)
- 頁面遮罩層,并且阻止頁面body滾動(dòng)。bootstrap模態(tài)框原理
相關(guān)文章
基于js實(shí)現(xiàn)逐步顯示文字輸出代碼實(shí)例
這篇文章主要介紹了基于js實(shí)現(xiàn)逐步顯示文字輸出代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04JavaScript識(shí)別網(wǎng)頁關(guān)鍵字并進(jìn)行描紅的方法
這篇文章主要介紹了JavaScript識(shí)別網(wǎng)頁關(guān)鍵字并進(jìn)行描紅的方法,通過字符串的遍歷、匹配及動(dòng)態(tài)添加等操作實(shí)現(xiàn)識(shí)別與描紅的功能,非常簡單實(shí)用,需要的朋友可以參考下2015-11-11JS函數(shù)多個(gè)參數(shù)默認(rèn)值指定方法分析
這篇文章主要介紹了JS函數(shù)多個(gè)參數(shù)默認(rèn)值指定方法,結(jié)合實(shí)例形式分析了javascript函數(shù)參數(shù)的定義與傳遞相關(guān)操作技巧,需要的朋友可以參考下2016-11-11JavaScript實(shí)現(xiàn)頁面實(shí)時(shí)顯示當(dāng)前時(shí)間的簡單實(shí)例
這篇文章介紹了頁面實(shí)時(shí)顯示當(dāng)前時(shí)間的簡單實(shí)例,有需要的朋友可以參考需要2013-07-07微信分享的標(biāo)題、縮略圖、連接及描述設(shè)置方法
微信分享的標(biāo)題、縮略圖、連接及描述該如何設(shè)置,這個(gè)問題確實(shí)難道不少朋友,下面有個(gè)示例,大家可以看看2014-10-10js,jq,css多方面實(shí)現(xiàn)簡易下拉菜單功能
這篇文章主要介紹了js,jq,css多方面實(shí)現(xiàn)簡易下拉菜單功能,需要的朋友可以參考下2017-05-05