您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何正確的使用Bootstrap3 模態框,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
不支持同時打開多個模態框
務必盡量將HTML代碼放置在模態框的body位置以避免其他組件影響模態框
*autofocus 對于模態框無效, 需要自己調用 $('#myModal').on('shown.bs.modal', function () { $('#myInput').focus() })*
實例
靜態
<div class="modal fade" tabindex="-1" role="dialog"> <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> <h5 class="modal-title">Modal title</h5> </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 changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
動態
<!-- Button trigger modal --> <button type="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"> <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> <h5 class="modal-title" id="myModalLabel">Modal title</h5> </div> <div class="modal-body"> ... </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 changes</button> </div> </div> </div> </div>
可選尺寸
<!-- Large modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-lg">Large modal</button> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> ... </div> </div> </div> <!-- Small modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">Small modal</button> <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"> <div class="modal-dialog modal-sm" role="document"> <div class="modal-content"> ... </div> </div> </div>
禁用fade
<div class="modal" tabindex="-1" role="dialog" aria-labelledby="..."> ... </div>
使用柵格系統
僅需在body中使用 .rows
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel"> <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> <h5 class="modal-title" id="gridSystemModalLabel">Modal title</h5> </div> <div class="modal-body"> <div class="row"> <div class="col-md-4">.col-md-4</div> <div class="col-md-4 col-md-offset-4">.col-md-4 .col-md-offset-4</div> </div> <div class="row"> <div class="col-md-3 col-md-offset-3">.col-md-3 .col-md-offset-3</div> <div class="col-md-2 col-md-offset-4">.col-md-2 .col-md-offset-4</div> </div> <div class="row"> <div class="col-md-6 col-md-offset-3">.col-md-6 .col-md-offset-3</div> </div> <div class="row"> <div class="col-sm-9"> Level 1: .col-sm-9 <div class="row"> <div class="col-xs-8 col-sm-6"> Level 2: .col-xs-8 .col-sm-6 </div> <div class="col-xs-4 col-sm-6"> Level 2: .col-xs-4 .col-sm-6 </div> </div> </div> </div> </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 changes</button> </div> </div><!-- /.modal-content --> </div><!-- /.modal-dialog --> </div><!-- /.modal -->
使用方法
通過data屬性
<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>
通過JavaScript調用
$('#myModal').modal(options)
參數
名稱 | 類型 | 默認值 | 描述 |
---|---|---|---|
backdrop | boolean 或 字符串 ‘static' | true | 設置為 static 時, 在點擊屏幕時不會關閉 |
keyboard | boolean | true | 鍵盤上的 esc 鍵被按下時關閉模態框 |
show | boolean | true | 模態框初始化之后就立即顯示出來 |
方法
模態框激活
.modal(options)
$('#myModal').modal({ keyboard: false })
模態框觸發手動打開或者關閉
觸發在 shown.bs.modal 以及 hidden.bs.modal 事件之前
.modal('toggle')
手動打開模態框
觸發在 shown.bs.modal 之前
$('#myModal').modal('show')
手動隱藏模態框
觸發在 hidden.bs.modal 之前
$('#myModal').modal('hide')
事件
按照時間先后順序分別為 show.bs.modal shown.bs.modal hide.bs.modal hidden.bs.modal
調用方式
$('#myModal').on('hidden.bs.modal', function (e) { // do something... })
上述內容就是如何正確的使用Bootstrap3 模態框,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。