bootstrap 多模态框实现

这个不是本人原创,不好意思,不记得哪位大神的作品了,在这里表示敬意

默认情况下,bootstrap模态框是不支持多个覆盖的,下面是一个解决办法(本人亲测),

将下面的代码复制到当前需要多个模态框的页面,问题就可以解决,如图:


$(document).on('show.bs.modal', '.modal', function(event) {
        $(this).appendTo($('body'));
    }).on('shown.bs.modal', '.modal.in', function(event) {
        setModalsAndBackdropsOrder();
    }).on('hidden.bs.modal', '.modal', function(event) {
        setModalsAndBackdropsOrder();
    });


    function setModalsAndBackdropsOrder() {  
        var modalZIndex = 1040;
        $('.modal.in').each(function(index) {
            var $modal = $(this);
            modalZIndex++;
            $modal.css('zIndex', modalZIndex);
            $modal.next('.modal-backdrop.in').addClass('hidden').css('zIndex', modalZIndex - 1);
        });
        $('.modal.in:visible:last').focus().next('.modal-backdrop.in').removeClass('hidden');
    }

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Bootstrap 是一个流行的前端框架,它提供了许多实用的组件,其中包括模态框。下面是使用 Bootstrap 实现模态框的基本步骤: 1. 引入 Bootstrap 样式文件和 JavaScript 文件,可以通过 CDN 或本地文件引入。 ```html <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script> ``` 2. 在 HTML 文件中添加一个触发模态框的按钮,使用 `data-toggle` 和 `data-target` 属性指定模态框的 ID。 ```html <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> 打开模态框 </button> ``` 3. 添加一个模态框的 HTML 结构,包括模态框的头部、主体和尾部,使用 `id` 属性指定模态框的 ID。 ```html <div class="modal fade" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">模态框标题</h5> <button type="button" class="close" data-dismiss="modal">×</button> </div> <div class="modal-body"> <p>模态框内容...</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div> </div> ``` 4. 完成上述步骤后,点击按钮即可触发模态框的显示。如果需要通过 JavaScript 控制模态框的显示和隐藏,可以使用 Bootstrap 提供的 JavaScript API,如下所示: ```javascript // 显示模态框 $('#myModal').modal('show'); // 隐藏模态框 $('#myModal').modal('hide'); ``` 以上就是使用 Bootstrap 实现模态框的基本步骤。需要注意的是,Bootstrap模态框有很多可选参数和事件,可以根据实际情况进行配置和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值