javascript - 为什么我的一些按钮在 html5 Canvas 上不能正常工作?

标签 javascript jquery css html html5-canvas

我正在尝试制作一些按钮来控制 Canvas 中的某些内容。我希望它们出现在 Canvas 的右侧,所以我使用 css 来设置样式和移动它们。我还制作了一个计时器,它在按下绿色按钮开始时启动,在按下完成时停止。我的问题是,当我移动到正确位置时,第一个按钮不起作用或按下。你知道为什么会这样吗??这是我的代码中执行我所说的部分。提前致谢!!!

document.body.style.backgroundColor = "#FAFAD2";

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

window.requestAnimFrame = (function(callback) {
  return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
    window.setTimeout(callback, 1000 / 60);
  };
})();

var startTime = Date.now();
var t;
var timer_is_on = 0;

function timedCount() {
  var elapsedTime = Date.now() - startTime;
  document.getElementById("timer").innerHTML = (elapsedTime / 1000).toFixed(3);
  t = setTimeout("timedCount()", 100);
}

function doTimer() {
  if (!timer_is_on) {
    timer_is_on = 1;
    timedCount();
  }
}

function stopCount() {
  clearTimeout(t);
  timer_is_on = 0;
}

$('#start').on('click', function() {
  doTimer();
});

$('#finish').on('click', function() {
  stopCount();
});
.timer {
  position: absolute;
  bottom: 55px;
  left: 600px;
}

.both,
.one,
.two,
.timer {
  display: block;
  padding: 30px;
  position: absolute;
  left: 600px;
}

.both button {
  background-color: #4CAF50;
  border: 1px solid black;
  margin-left: 50px;
  color: white;
  padding: 15px 10px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 100px;
  display: block;
  position: relative;
  bottom: 20px;
}

.one button {
  border: 1px solid black;
  color: white;
  padding: 15px 5px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 200px;
  display: block;
  position: relative;
  top: 100px;
}

.two button {
  border: 1px solid black;
  color: white;
  padding: 15px 5px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 200px;
  display: block;
  position: relative;
  top: 300px;
}

.timer button {
  background-color: #696969;
  border: 1px solid black;
  color: white;
  padding: 12px 8px;
  text-align: center;
  font-size: 15px;
  font-weight: bold;
  cursor: pointer;
  width: 80px;
}
<!DOCTYPE html>
<html>

<head>
  <title> Simulation </title>

</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <div class='both'>
    <button type='button' id='start'>START</button>
    <button type='button' id='stop'>STOP</button>
  </div>
  <div class='one'>
    <button type='button' id='start1' style='background-color:#B22222;'>START 1</button>
    <button type='button' id='stop1' style='background-color:#B22222;'>STOP 1</button>
    <button type='button' id='position1' style='background-color:#B22222;'>POSITION 1</button>
  </div>
  <div class='two'>
    <button type='button' id='start2' style='background-color:#FFD700;'>START 2</button>
    <button type='button' id='stop2' style='background-color:#FFD700;'>STOP 2</button>
    <button type='button' id='position2' style='background-color:#FFD700;'>POSITION 2</button>
  </div>
  <div class='timer'>
    <span id="timer"></span> sec
    <button type='button' id='finish'>FINISH</button>
  </div>
  <br>
  <canvas id="myCanvas" width="455" height="650" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.</canvas>



</body>

</html>

最佳答案

尝试将 z-index 添加到按钮包装器和 Canvas 的相对位置

.both {z-index:99;} Canvas {position:relative;z-index:10;}

关于javascript - 为什么我的一些按钮在 html5 Canvas 上不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136376/

相关文章:

HTML Table 自动开始将行拆分为新的列

javascript - Scriptaculous Effect.Grow() 从设定大小开始,而不是 0

javascript - jQuery 从字符串调用函数

javascript - 尾递归二叉树搜索函数JS

javascript - 如何将带有动画的 div 添加到网页?

javascript - 根据另一个值存储数组的项目

css - 如何禁用每个 TouchableOpacity 上的双边框?

asp.net - CSS 问题 : google Chrome not displaying the back groung Image and hence Drop down list not getting displayed properly

jquery - 单击打开另一个下拉菜单时关闭打开的下拉菜单

javascript - 检查元素的文本是否与另一个元素的文本相同