bootstrap 模态框插件

Bootstrap 模态框(Modal)插件

模态框(Modal)是覆盖在父窗体上的子窗体

1. 基本使用
  • 通过 data 属性:在控制器元素上设置属性 data-toggle=“modal”,同时设置 data-target="#identifier" 来控制是操作哪个模态框。
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    打开模态框
</button>
  • 通过 JavaScript:使用这种技术,可以通过简单的一行 JavaScript 来调用带有 id=“identifier” 的模态框:
// 点击提交更改的同时关闭模态框
function handleSubmit() {
    
    
    $('#myModal').modal('hide')
}

例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Bootstrap 实例 - 模态框(Modal)插件</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<!-- 按钮触发模态框 -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
    打开模态框
</button>

<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="true"
     data-keyboard="true" data-show="true"
>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    模态框(Modal)标题
                </h4>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="handleSubmit()">
                    提交更改
                </button>
            </div>
        </div>
    </div>
</div>

<script>
    // 点击提交更改的同时关闭模态框
    // function handleSubmit() {
     
     
    //     $('#myModal').modal('hide')
    // }
    $('#myModal').on('show.bs.modal', function () {
     
     
        // 执行一些动作...
        console.log('打开了模态框,可以执行一些操作。。。')
    })

    $('#myModal').on('hide.bs.modal', function () {
     
     
        // 执行一些动作...
        console.log('关闭了模态框,可以执行一些操作。。。')
    })
</script>
</body>
</html>
2. 代码解读
  1. .modal,用来把
    的内容识别为模态框。
  2. 第二是 .fade。当模态框被切换时,它会引起内容淡入淡出。
  3. aria-labelledby=“myModalLabel”,该属性引用模态框的标题。
  4. aria-hidden=“true” 用于保持模态窗口不可见,直到触发器被触发为止。
  5. **
  6. class=“close”,close 是一个 CSS class,用于为模态窗口的关闭按钮设置样式。
  7. data-dismiss=“modal”,用于关闭模态窗口。
  8. class=“modal-body”,是 Bootstrap CSS 的一个 CSS class,用于为模态窗口的主体设置样式。
  9. class=“modal-footer”,是 Bootstrap CSS 的一个 CSS class,用于为模态窗口的底部设置样式。
  10. data-toggle=“modal”, 用于打开模态窗口。
  11. data-backdrop=“static”, 当用户点击模态框外部时不会关闭模态框。去掉该属性,点击模态框外部会关闭模态框
  12. data-keyboard=“true”, 为true时,esc键可以退出模态框; false时不可以
  13. data-show=“false” 为false时,第一次点击打开模态框不会起作用; true 会起作用
3. 事件
  1. 打开模态框执行
$('#myModal').on('show.bs.modal', function () {
    
    
    // 执行一些动作...
    console.log('打开了模态框,可以执行一些操作。。。')
})
  1. 关闭模态框
$('#myModal').on('hide.bs.modal', function () {
    
    
    // 执行一些动作...
    console.log('关闭了模态框,可以执行一些操作。。。')
})

更多参考: https://www.runoob.com/bootstrap/bootstrap-modal-plugin.html

猜你喜欢

转载自blog.csdn.net/gklcsdn/article/details/115000157