Bootstrap 的模態框(Modal)需要三層 div 容器,由外而內分別是:
modal(模態宣告層)→ dialog(視窗宣告層)→ content(內容層)
而在內容層裡面,又分三個區塊:header(頭部)、body(主體)、footer(註腳)。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="./css/bootstrap.min.css"> <script src="./js/jquery.min.js"></script> <script src="./js/bootstrap.min.js"></script> <style> .modal-dialog { width: 20%; } .modal-footer, .modal-header { text-align: center; } input { width: 80%; } </style> </head> <body> <!-- LOGIN MODULE --> <div id="loginModal" class="modal fade" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span>×</span> </button> <h4 class="modal-title">會員登錄</h4> </div> <div class="modal-body"> <label for="log_uname"> <span>帳號:</span> <input id="log_uname" name="log_uname" type="text" placeholder="input your account"> </label> <br> <label for="log_passwd"> <span>密碼:</span> <input id="log_passwd" name="log_passwd" type="password" placeholder="input your password"> </label> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary">登錄</button> <button type="button" class="btn btn-warning" data-dismiss="modal">退出</button> </div> </div> </div> </div> <!-- LOGIN MODULE --> <div id="registerModal" class="modal fade" tabindex="-1"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"> <span>×</span> </button> <h4 class="modal-title">注冊會員</h4> </div> <div class="modal-body"> <label for="uname"> <span>帳號:</span> <input id="reg_uname" name="reg_uname" type="text" placeholder="input your account"> </label> <br> <label for="reg_passwd"> <span>密碼:</span> <input id="reg_passwd" name="reg_passwd" type="password" placeholder="input your password"> </label> <label for="reg_confirm_passwd"> <span>確認:</span> <input id="reg_confirm_passwd" name="reg_confirm_passwd" type="password" placeholder="confirm your password"> </label> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary">注冊</button> <button type="button" class="btn btn-warning" data-dismiss="modal">退出</button> </div> </div> </div> </div> <button class="btn btn-primary" data-toggle="modal" data-target="#loginModal">登陸</button> <button class="btn btn-warning" data-toggle="modal" data-target="#registerModal">注冊</button> </body> </html>
觸發方式很直覺:在按鈕上加 data-toggle="modal" 與 data-target="#模態框ID" 即可;關閉則在元素上加 data-dismiss="modal"。
$('#myModal').modal({ show : true, backdrop : false, keyboard : false, remote : 'index.html', });
$('#myBtn').on('click', function () { $('#myModal').modal('show'); });
show.bs.modal → 呼叫 show 方法時立即觸發
shown.bs.modal → 模態框完全顯示且 CSS 動畫結束後觸發
hide.bs.modal → 呼叫 hide 方法時、尚未關閉前觸發
hidden.bs.modal → 模態框完全隱藏且動畫結束後觸發
$('#myModal').on('show.bs.modal', function () { alert('show !'); });
命名規則是「時機.bs.元件名」,第二段的 bs 是命名空間,可以用來只解除該元件的事件綁定而不影響其他。
<button class="btn btn-lg btn-danger" type="button" data-toggle="popover" title="彈出框" data-content="這是一個彈出框">點擊彈出/隱藏彈出框</button> <script> $('button').popover(); </script>
可用方法:
$('button').popover('show'); //顯示 $('button').popover('hide'); //隱藏 $('button').popover('toggle'); //反轉顯示和隱藏 $('button').popover('destroy'); //隱藏並銷毀
對應事件:
show.bs.popover → 呼叫 show 方法時觸發
shown.bs.popover → 整個彈窗顯示完成時觸發
hide.bs.popover → 呼叫 hide 方法時觸發
hidden.bs.popover → 整個彈出完全關閉時觸發
想深入學習的話,可以搭配官方文件一起看,效果會更好。