jquery - 当一个模式关闭并立即打开另一个模式时,如何解决 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">×</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>
解决方案
您可以收听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">×</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>
推荐阅读
- javascript - 如何使用 d3 使用 javascript 可视化本地 csv 文件?
- php - Codeigniter 应用程序中大量 mpdf 事务的服务器超时
- java - 使用名称创建 bean 时出错:注入自动装配的依赖项失败,无法解析占位符
- apache - 在 solr 服务器重新启动时丢失 solr 数据
- node.js - 使用 Mocha 对在 AMQP 消息接收事件时应触发的回调进行集成测试
- python - 使用“SearchFilter”时,尝试搜索时出现错误
- visual-c++ - 使用构造函数在类中初始化字符串
- groovy - Katalon Studio 从其他自定义关键字调用自定义关键字
- google-drive-api - google drive API的测试环境?
- vb.net - 使用第二种形式作为“请稍候......”-信息:奇怪的外观