首页 > 解决方案 > 当一个模式关闭并立即打开另一个模式时,如何解决 Bootstrap 4 中的滚动问题?

问题描述

我的应用程序中有代码需要关闭现有的modal并立即打开另一个modal. 该代码工作正常,但scrolling issue第二个模态有一个(垂直/水平)。如果我调整屏幕大小并尝试垂直滚动,则只有背景内容在滚动。我一直在挖掘问题,这就是我发现的。我认为存在时间竞争条件问题。如果我把这setTime {...some code...,400}将解决问题。我仍然相信有一个更清洁的解决方案。此外,如果有人可以解释为什么会发生这种情况,那就太好了。这是代码示例:

var COMMON_FUNC = {};
COMMON_FUNC.dialogBox = function(title, message, size) {
  title = title || 'HCS System';
  message = message || 'HCS Dialog Box';
  size = size || 'lg';

  var dialog = bootbox.dialog({
    onEscape: true,
    backdrop: true,
    size: size,
    title: '<strong>' + title + '</strong>',
    message: message
  });
  dialog.prop("id", "dialog-box");
};

$("#open-modal-one").on("click", function() {
  var div = '<div><button type="button" class="btn btn-sm btn-secondary edit-record">Edit</button></div>'
  COMMON_FUNC.dialogBox("Modal One", div, "xl");
});

$(document).on("click", ".edit-record", function() {
  $("#dialog-box").modal("hide");
  //setTimeout(function(){$("#second_modal").modal("show");},400);
  $("#second_modal").modal("show");
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.4.0/bootbox.min.js"></script>

<h1>This is a test!</h1>
<button type="button" class="btn btn-sm btn-secondary" id="open-modal-one">Open Modal One</button>
<div class="modal fade" id="second_modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Second Modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        This is second modal. If I try to scroll vertically only background will scroll.
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary">Apply</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>

标签: jquerycssbootstrap-4bootstrap-modal

解决方案


您可以收听hide第一个模式的事件,并且只有在完成后才能显示第二个。

$(document).on("click", ".edit-record", function() {
  $("#dialog-box").one('hidden.bs.modal', function () {
    $("#second_modal").modal("show");
  }).modal("hide");
});

像这样:

var COMMON_FUNC = {};
COMMON_FUNC.dialogBox = function(title, message, size) {
  title = title || 'HCS System';
  message = message || 'HCS Dialog Box';
  size = size || 'lg';

  var dialog = bootbox.dialog({
    onEscape: true,
    backdrop: true,
    size: size,
    title: '<strong>' + title + '</strong>',
    message: message
  });
  dialog.prop("id", "dialog-box");
};

$("#open-modal-one").on("click", function() {
  var div = '<div><button type="button" class="btn btn-sm btn-secondary edit-record">Edit</button></div>'
  COMMON_FUNC.dialogBox("Modal One", div, "xl");
});

$(document).on("click", ".edit-record", function() {
  $("#dialog-box").one('hidden.bs.modal', function () {
    $("#second_modal").modal("show");
  }).modal("hide");
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/5.4.0/bootbox.min.js"></script>

<h1>This is a test!</h1>
<button type="button" class="btn btn-sm btn-secondary" id="open-modal-one">Open Modal One</button>
<div class="modal fade" id="second_modal" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Second Modal</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
        This is second modal. If I try to scroll vertically only background will scroll.
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary">Apply</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
      </div>
    </div>
  </div>
</div>


推荐阅读