bootstrap 多层modal关闭底层modal滚动条失效问题

bootstrap 多层modal关闭底层modal滚动条失效问题

原文地址:https://www.iteye.com/blog/oedy-2376519

bootstrap中如果在模态框弹出模态框,即多重模态框,那么在关闭模态框后,上一级的模态框无法滚动,而且滚动事件会穿透到body层。

 

原理是只要有modal被打开,body会被赋予modal-open这个类,使其overflow变为hidden,无法滚动;而只要有modal被关闭,body的modal-open这个类就会被remove掉。

 

解决方案:在非最上层modelmodal监听隐藏事件,为body重新添加modal-open类:

$(w).on("hidden.bs.modal", function () {

        $(this).removeData("bs.modal");

        $(document.body).addClass("modal-open");  

        $(w).find("div.modal-content").html("");

    });

 

model封装:

function openModal(url, width, height, popId, callBack) {

    if (!popId) {

        var random = Math.floor(Math.random() * 100);

        popId = "viewWindw_" + random;

    }

    var templet = '<div id="' + popId + '" class="modal fade" aria-hidden="true" aria-labelledby="' + popId + 'Label' + '">'

        + '<div class="modal-dialog" role="document">'

        + '<div class="modal-content">'

        + '<div class="modal-body"></div>'

        + '</div>'

        + '</div>'

        + '</div>';

    $(document.body).append(templet);

    if (height)

        $('.modal-dialog').height(height);

    if (width)

        $('.modal-dialog').width(width);

 

    var w = "#" + popId;

    $(w).modal({

        remote: url

    });

 

    $(w).on("hidden.bs.modal", function () {

        $(this).removeData("bs.modal");

        $(w).find("div.modal-content").html("");

    });

 

    if (null != callBack && isFunction(callBack)) {

        $(w).on("loaded.bs.modal", callBack);

    }

    return w;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值