gpt4 book ai didi

javascript - 尝试通过 javascript 呈现 Bootstrap 模式时未定义

转载 作者:行者123 更新时间:2023-11-28 04:26:32 24 4
gpt4 key购买 nike

所以,我正在研究 <canvas>在 Bootstrap 模式中创建和呈现的游戏。我正在尝试编写一个将在 gameOver 上调用的 javascript 函数,该函数将在 Canvas 顶部呈现一个新的(堆叠的)模式,其中包含 3 个用于分数提交/注册/重播的按钮。

我通过 data-toggle 展示我的第一个模态在我的 html 中并且工作正常,但是当我尝试在顶部呈现我的第二个模式(通过 javascript)时,它似乎被报告为未定义。

这是我的代码:JSFiddle .
(在本例中,我在 Canvas 绘制方法中调用第二个模态作为示例,但我也尝试了单独的按钮、其他事件监听器等。问题始终是相同的)。

如果有更好的方式来呈现带有按钮的游戏结束屏幕,我愿意接受建议。我想避免在 Canvas 上绘制任何按钮并获取坐标来读取其上的点击。

提前致谢。

最佳答案

如果您想在另一个模态框之上打开模态框,则必须调整z-index。使用此处提供的解决方案Multiple modals overlay ,你可以做这样的事情。

$(document).ready(function() {

let canvas = null;
let drawCanvas = () => {
canvas = document.getElementById("myCanvas");
const context = canvas.getContext("2d");
context.fillStyle = "#FF0000";
context.fillRect(0, 0, 630, 850);

};
$('#openBtn').click(function() {
$('#myModal').modal({
show: true
});
if (!canvas) {
drawCanvas();
}
setTimeout(function() {
$('#myModal2').modal({
show: true,
});
}, 2000)
});

$(document).on('show.bs.modal', '.modal', function(event) {
var zIndex = 1040 + (10 * $('.modal:visible').length);
$(this).css('z-index', zIndex);
setTimeout(function() {
$('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
}, 0);
});


});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h2>Stacked Bootstrap Modal Example.</h2>
<a id="openBtn" class="btn btn-primary">Launch modal</a>

<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal 1</h4>

</div>
<div class="modal-body">
<canvas id="myCanvas" width="630" height="850">
</canvas>
<a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a>

</div>
<div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>

</div>
</div>
</div>
</div>
<div class="modal fade rotate" id="myModal2">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal 2</h4>

</div>
<div class="container"></div>
<div class="modal-body">Content for the dialog / modal goes here.
<br>
<br>
<p>come content</p>
<br>
<br>
<br> <a data-toggle="modal" href="#myModal3" class="btn btn-primary">Launch modal</a>

</div>
<div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>

</div>
</div>
</div>
</div>
<div class="modal fade" id="myModal3">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal 3</h4>

</div>
<div class="container"></div>
<div class="modal-body">Content for the dialog / modal goes here.
<br>
<br>
<br>
<br>
<br> <a data-toggle="modal" href="#myModal4" class="btn btn-primary">Launch modal</a>

</div>
<div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>

</div>
</div>
</div>
</div>
<div class="modal fade" id="myModal4">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal 4</h4>

</div>
<div class="container"></div>
<div class="modal-body">Content for the dialog / modal goes here.</div>
<div class="modal-footer"> <a href="#" data-dismiss="modal" class="btn">Close</a>
<a href="#" class="btn btn-primary">Save changes</a>

</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</body>
</html>

关于javascript - 尝试通过 javascript 呈现 Bootstrap 模式时未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com