简体   繁体   English

Bootstrap模态在切换,后台滚动时不变为活动元素,但模态不滚动

[英]Bootstrap modal not becoming active element on toggle, background scrolling but modal not scrolling

I have a web page that makes use of a number of different modals to display information to the user. 我有一个网页,该网页利用许多不同的模式向用户显示信息。

These modals are triggered by certain buttons, which usually call $("#id").modal("toggle") to toggle the modal's visibility on. 这些模态由某些按钮触发,这些按钮通常调用$("#id").modal("toggle")来打开模态的可见性。 In one particular scenario, I have one modal which displays a second modal through the use of the above stated function. 在一个特定的场景中,我有一个模态,它通过使用上述函数显示第二个模态。 It also hides itself through the use of the same function, so I have an onClick function that does the following. 它也通过使用相同的函数来隐藏自身,因此我有一个执行以下操作的onClick函数。

$("#EditTask").modal("hide");
$("#AddressProspect").modal("show");

The issue is that when the AddressProspect modal is displayed, it seems as though it is not being changed to the active element. 问题在于,当显示AddressProspect模式时,好像没有将其更改为活动元素。 The background goes dark, and the modal is displayed correctly. 背景变暗,并且模态正确显示。 However when I attempt to scroll, the background elements scroll instead, as if the modal hasn't actually been displayed. 但是,当我尝试滚动时,背景元素会滚动,就像实际未显示模式一样。

I have attempted a number of different approaches. 我尝试了多种不同的方法。 I have used .modal("show") and .modal("hide") to display the modals I need. I have also tried 我已经使用.modal("show").modal("hide") to display the modals I need. I have also tried .modal("hide") to display the modals I need. I have also tried data-dismiss="modal" within the button of the modal that needs to be hidden. .modal("hide") to display the modals I need. I have also tried了需要隐藏的模态按钮内的data-dismiss =“ modal”。 These have all produced the exact same result. 这些都产生了完全相同的结果。

Interestingly, if I go to my console and execute the following commands 有趣的是,如果我进入控制台并执行以下命令

$("body").css("overflow-y", "hidden");
$("#AddressProspect").css("overflow-y", "scroll");

The background becomes unscrollable, and the AddressProspect modal becomes scrollable, just as I would expect. 就像我期望的那样,背景变得不可滚动,而AddressProspect模态也变得可滚动。

I have around 10 modals being used within my page, and none of them have this problem apart from the one in question. 我的页面中使用了大约10个模态,除了所讨论的模态之外,这些模态都没有。 I have posted the code to the two modals mentioned in this post below, with their bodies removed for clarity. 我已将代码发布到下面这篇文章中提到的两个模式中,为了清楚起见,删除了它们的主体。

<div class="modal fade bd-example-modal-lg" id="EditTask" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel"><span class="Title">Title</span></h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">

                    -snip-

            </div>
            <div class="modal-footer">
                <div style="width: 100%">
                    <button type="button" class="btn btn-warning action-skip-instruction" style="float: left;" data-dismiss="modal">Skip Instruction</button>
                </div>

                <button type="button" class="btn btn-secondary action-close-edit-modal">Close</button>
                <button type="button" id="edit-task-button-defer" class="btn btn-warning edit-task-action" style="display: none;">Defer</button>
                <button type="button" id="edit-task-button-action" class="btn btn-success edit-task-action" data-dismiss="modal">Complete</button>

            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="AddressProspect" tabindex="-1" role="dialog" data-keyboard="false" data-backdrop="static" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="max-width: 600px;" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">New Security Address Prospect</h5>
                </div>

                <div class="modal-body">

                    -snip-

                </div>
                <div class="modal-footer">
                    <div style="width: 100%">
                        <button type="button" class="btn btn-warning prospect-button-clear" style="float: left;">Clear</button>
                    </div>

                    <button type="button" style="float: left;" class="btn btn-danger prospect-button-close" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-success prospect-button-update">Update</button>
                </div>
            </div>
        </div>
    </div>

After a decent amount of time having issues with this problem I have managed to figure out why it is happening and how to fix it. 经过一段时间的解决后,我设法弄清了为什么会这样以及如何解决它。

There are two places I looked for information on this topic: 我在两个地方寻找有关此主题的信息:

This Github issue on the Bootstrap repo Bootstrap回购上的这个Github问题

The Bootstrap documentation on modal methods 有关模态方法的Bootstrap文档

From reading the Github issue we can see that one user notes that the calls to .modal("hide") and .modal("show") are asynchronous, therefore using them in quick succession is invalid. 通过阅读Github问题,我们可以看到一个用户注意到对.modal("hide").modal("show")的调用是异步的,因此快速连续使用它们是无效的。 Instead we must wait for the show/hide to complete by listening for the shown or hidden events. 相反,我们必须通过侦听shownhidden事件来等待显示/隐藏完成。

To do this we can use the following function: 为此,我们可以使用以下功能:

$('#myModal').on('hidden.bs.modal', function (e) {
  // do something...
})

By checking for the hidden.bs.modal class to be placed into the modal's classlist we can verify when the hide animation has completely finished. 通过检查hidden.bs.modal类是否放入模态的类列表中,我们可以验证hide动画何时完全完成。 At this point we can then call the show method of the next modal to be displayed, ensuring that all of the background events handled by Bootstrap have finished executing, and ensuring that behaviour is expected. 此时,我们可以调用下一个要显示的模式的show方法,以确保由Bootstrap处理的所有后台事件均已完成执行,并确保预期的行为。

My previous code goes from being: 我以前的代码来自:

$("#EditTask").modal("hide");
$("#AddressProspect").modal("show");

to: 至:

$("#EditTask").modal('toggle');
$("#EditTask").on("hidden.bs.modal", function() {
      $("#AddressProspect").modal('toggle');        
      });

As soon as I tried the new code, the weird issues with scrolling disappeared. 我尝试使用新代码后,滚动问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM