Bootstrap模态框问题 - 滚动被禁用

132

我有一个模态框,在这个模态框中,有一个下拉菜单显示大量隐藏内容,目前它是正常工作的。

现在当你打开下一个模态框、叠在第一个模态框之上并关闭它时,位于下面的模态框上的滚动变为禁用状态。

我已创建了一个完整的示例,包括重现我遇到的问题的步骤。您可以在此处查看。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
    <title></title>
    <style>

    </style>
</head>
<body>


    <input type="button" data-toggle="modal" data-target="#modal_1" class="btn btn-lg btn-primary" value="Open Modal 1" >

    <div class="modal fade" id="modal_1">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">First Modal</h4>
                </div>
                <div class="modal-body">



                    <form class="form">

                        <div class="form-group">
                            <label>1) Open This First: </label>
                            <input type="button" data-toggle="modal" data-target="#modal_2" class="btn btn-primary" value="Open Modal 2" >
                        </div>

                        <div class="form-group">
                            <label>2) Change this once you have opened the modal above.</label>
                            <select id="change" class="form-control">
                                <option value="small">Show Small Content</option>
                                <option value="large">Show Large Content</option>
                            </select>
                        </div> 

                        <div id="large" class='content-div'>
                            <label>Large Textarea Content.. Try and scroll to the bottom..</label>
                            <textarea rows="30" class="form-control"></textarea>

                        </div>

                        <div id="small" class='content-div'>
                            <label> Example Text Box</label> 
                            <input type="text" class="form-control">
                        </div>  


                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>


    <div class="modal fade" id="modal_2">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">Second Modal</h4>
                </div>
                <div class="modal-body">

                    <hp>This is the stacked modal.. Close this modal, then chenge the dropdown menu, you cannot scroll... </h5>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>


</body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" type="text/javascript"></script>
<script>

    $(document).ready(function() {


        $(".content-div").hide();
        $("#change").change(function() {
            $(".content-div").hide();
            $("#" + $(this).val()).show();
        });


    });

</script>
</html>

这里有一个 Bootply,展示该行为的效果:

Bootply


1
请确保在另一个模态框仍可见时不要打开模态框。同时显示多个模态框需要自定义代码,这也违反了模态框的设计目的。 - Blazemonger
1
请参见 https://dev59.com/S2Ik5IYBdhLWcg3wadcD#24914782,其中包含了不仅针对滚动条问题而且还针对样式问题的修复方法。 - benrwb
27个回答

176

这也是一个解决方案

.modal {
  overflow-y:auto;
}

http://www.bootply.com/LZBqdq2jl5

自动运行良好 :) 这实际上可以修复任何比您的屏幕尺寸更高的模态框。


11
这个解决方案解决了我的问题,它使用 jQuery 的 $('.modal').css('overflow-y', 'auto'); 在打开和关闭模态框时生效。 - Gli
生命救星)谢谢 - Victor Gorban

97
这是因为当您关闭模态时,它会从标签中删除modal-open。Bootstrap不支持在同一页上显示多个模态(至少到BS3版本为止)。
使其正常工作的一种方法是使用Bootstrap触发的hidden.bs.modal事件来关闭模态,然后检查是否有任何其他模态打开,以便强制将modal-open类添加到body中。
// Hack to enable multiple modals by making sure the .modal-open class
// is set to the <body> when there is at least one modal open left
$('body').on('hidden.bs.modal', function () {
    if($('.modal.in').length > 0)
    {
        $('body').addClass('modal-open');
    }
});

19
(BS4)我能得到的第一个答案,只需要在Bootstrap 4中使用($('.modal.show')。length> 0)。 - Shoejep
1
谢谢 - 我的问题不完全相同,但是你的评论指出了 body 上的 modal-open 类是导致页面滚动失效的原因。 - Jon
2
最佳答案 - Renan
1
这实际上是我在Stack Overflow中找到的最佳答案。 - Yasser CHENIK
非常好!我刚刚将它添加到我的app.js(BS5)中,它运行得非常漂亮。 - Mauritz Hansen

50

我已经为您找到了解决方案。不确定为什么它不工作,但是您的CSS中只需要一行代码即可解决问题。在关闭第二个模态框之后,第一个模态框会以某种方式变为 overflow-y:hidden ,即使实际上设置为auto

您需要重新编写并在CSS中设置自己的声明:

#modal_1 {
    overflow-y:scroll;
}

这里有一个工作的演示

编辑:链接错误,抱歉。


无法工作,它使容器可滚动,但也会使鼠标滚轮卡住。 - Kiran Maniya

26
$(document).ready(function () {

 $('.modal').on("hidden.bs.modal", function (e) { //fire on closing modal box
        if ($('.modal:visible').length) { // check whether parent modal is opend after child modal close
            $('body').addClass('modal-open'); // if open mean length is 1 then add a bootstrap css class to body of the page
        }
    });
});
//this code segment will activate parent modal dialog 
//after child modal box close then scroll problem will automatically fixed

3
我认为这是最佳答案。添加额外的CSS以实现overflow-y: auto也可以,但正如下面所提到的那样,会导致双滚动条。 - Jacob Rutherford
2
也适用于Bootstrap 4.1。感谢。 - Willi
2
同意。测试了2-3个答案,这个适用于Twitter Bootstrap 4.2.1。 - Tpojka

18

对于Bootstrap 4,我不得不添加!Important

.modal {
    overflow-y: auto !important;
}
如果不这样做,它将无法运行并且不会被应用。 screenshot

对我而言,这只是一个半成品解决方案,因为我需要点击模态框以重新获得滚动能力。@ashish dwivedi的解决方案对我完美地起作用了。 - Matthieu Charbonnier

15

2
这个解决方案会创建两个滚动条。 - Weijing Jay Lin
这里有很多解决方案都被认为是有效的,我选择了这个因为它很简单。对我来说完美地运作了。点赞。 - kapitan

14

Bootstrap不支持同时使用多个模态框,这是Bootstrap的一个Bug。只需添加以下脚本即可解决BS4问题。

$('body').on('hidden.bs.modal', function () {
if($('.modal.show').length > 0)
{
    $('body').addClass('modal-open');
}
});

1
非常好用。但请确保将此代码放在您的模态框之后,或者在 $(document).ready 方法中使用。 - Matthieu Charbonnier

10
$('.modal').css('overflow-y', 'auto');

7
首先,Bootstrap不支持多个打开的模态框。请参阅此处:Bootstrap Modal文档

不支持多个打开的模态框。 确保不要在另一个模态框仍然可见时打开模态框。同时显示多个模态框需要自定义代码。

但是,如果必须这样做,您可以在自定义样式表中添加此CSS代码片段,只要它在主Bootstrap样式表之包含:
.modal {
    overflow-y:auto;
}

3
我通过移除data-dismiss="modal"并添加来解决问题。
setTimeout(function(){ $('#myModal2').modal('show') }, 500);
$('#myModal1').modal('hide');

希望这有所帮助。

谢谢 @Sunny,我不知道为什么其他解决方案对我无效,但这个肯定有效。 - dev-masih
@MasihAkbari 可能是版本问题 :) 这个也帮了我。我正在使用BS4。由于延迟可能会有一些性能问题,但这是一个快速简单的解决方法。 - Weijing Jay Lin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接