bootstrap中的模態框(modal,彈出層)


默認的modal示例:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Modal</title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    Launch demo modal
</button>
 
<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>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </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</button>
            </div>
        </div>
    </div>
</div>
 
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
  1. 為 .modal 添加 role="dialog",用於指定模態框為對話框。
  2. 為 .modal-dialog 添加 aria-hidden="true" 屬性。
  3. 通過 aria-describedby 屬性為模態框 .modal 添加描述性信息。

關閉動畫

如果你不需要模態框彈出時的動畫效果(淡入淡出效果),刪掉 .fade 類即可。

調用模態框一:

<button type="button" class="list-group-item" data-toggle="modal" data-target="#myModal" data-whatever="測試">測試</button>

調用模態框二:

1、手動打開或關閉模態框。在模態框顯示或隱藏之前返回到主調函數中(也就是,在觸發 shown.bs.modal 或 hidden.bs.modal 事件之前)。

$('#myModal').modal('toggle')

2、手動打開模態框。在模態框顯示之前返回到主調函數中 (也就是,在觸發 shown.bs.modal 事件之前)。

$('#myModal').modal('show')

3、手動隱藏模態框。在模態框隱藏之前返回到主調函數中 (也就是,在觸發 hidden.bs.modal 事件之前)。

$('#myModal').modal('hide')

4、更新模態框,在模態框動態添加或刪除內容時:

 $('#myModal').modal('handleUpdate')

例:
//會議簽到事件
    function signButton(confId) {
        $('#myModal_sing').modal('toggle');
        event.stopPropagation();
    }

 

事件

下表列出了模態框中要用到事件。這些事件可在函數中當鈎子使用。

事件 描述 實例
show.bs.modal 在調用 show 方法后觸發。
$('#identifier').on('show.bs.modal', function () { // 執行一些動作... })
shown.bs.modal 當模態框對用戶可見時觸發(將等待 CSS 過渡效果完成)。
$('#identifier').on('shown.bs.modal', function () { // 執行一些動作... })
hide.bs.modal 當調用 hide 實例方法時觸發。
$('#identifier').on('hide.bs.modal', function () { // 執行一些動作... })
hidden.bs.modal 當模態框完全對用戶隱藏時觸發。
$('#identifier').on('hidden.bs.modal', function () { // 執行一些動作... })
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM