点击使用Javascript后,如何使形状改变颜色?

时间:2016-05-01 03:06:10

标签: javascript html css colors

我正在开展一个项目,正在努力创建一个简单的反应游戏。

为了让它更有趣,我想让形状在点击后改变颜色。我研究了这个问题的多种解决方案,但所有这些都需要刷新页面。我也不了解如何将其附加到特定形状,而不是整个页面的背景颜色。

这是我的代码......

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf=8">
    <style>
        #shape {
            width: 200px;
            height: 200px;
            background-color: red;
            display: none;
            position: relative;
        }

        body {
            font-family: sans-serif;
        }

        h1 {
            position: relative;
            top: -20px;
        }

        p {
            position: relative;
            top: -30px;
        }

        #yourTime {
            font-weight: bold;
        }
    </style>
</head>

<body>
    <h1 id="h1">Test Your Reactions!</h1>
    <p>Click on the squre or circle as soon as it appears!</p>
    <p id="yourTime"> Your time: <span id="timeTaken"></span></p>
    <div id="shape"></div>
    <script type="text/javascript">
        var start = new Date().getTime();

        function makeShapeAppear()
        {
            var top = Math.random() * 400;
            var left = Math.random() * 700;
            var width = (Math.random() * 200) + 100;
            if (Math.random() > 0.5) {
                document.getElementById("shape").style.borderRadius = "50%";
            } else {
                document.getElementById("shape").style.borderRadius = "0"
            }
            document.getElementById("shape").style.backgroundColor = "red";
            document.getElementById("shape").style.width = width + "px";
            document.getElementById("shape").style.height = width + "px";
            document.getElementById("shape").style.top = top + "px";
            document.getElementById("shape").style.left = left + "px";
            document.getElementById("shape").style.display = "block"
            start = new Date().getTime();
        }

        function appearAfterDelay()
        {
            setTimeout(makeShapeAppear, Math.random() * 1000);
        }

        appearAfterDelay()

        document.getElementById("shape").onclick = function()
        {
            document.getElementById("shape").style.display = "none";
            var end = new Date().getTime();
            var timeTaken = (end - start) / 1000;
            document.getElementById("timeTaken").innerHTML = timeTaken + "s"
            appearAfterDelay();
        }
    </script>
</body>

</html>

正如您所看到的,每次点击时,形状都应该将其颜色更改为不同的颜色。请帮我弄清楚每次如何将颜色改为随机颜色。

欢迎任何帮助和建议! (请原谅我,如果我没有正确的措辞或问题,这是我在Stack Overflow上的第一篇文章,所以随着时间的推移我会好起来!)

2 个答案:

答案 0 :(得分:1)

可以使用以下功能生成随机颜色...

function getRandomColor()
{
  var letters = '0123456789ABCDEF'.split('');

  var color = '#';

  for (var i = 0; i < 6; i++ )
  {
      color += letters[Math.floor(Math.random() * 16)];
  }

  return color;
}

您可以在脚本声明后启动变量...

<script type="text/javascript">

var start = new Date().getTime();

function getRandomColor()
{
  var letters = '0123456789ABCDEF'.split('');

  var color = '#';

  for (var i = 0; i < 6; i++ )
  {
      color += letters[Math.floor(Math.random() * 16)];
  }

  return color;
}

然后,您必须与getRandomColor()

交换“红色”
document.getElementById("shape").style.backgroundColor = getRandomColor();

如果您有任何其他问题,请询问!

答案 1 :(得分:0)

background-color可能是形状的背景,它不一定是页面的背景。这通常是用于进行div更改颜色的样式属性。

&#13;
&#13;
var colors = ["blue", "yellow", "green", "purple", "rgb(250, 175, 72)"];
var shapes = document.querySelectorAll(".shape");
Array.prototype.forEach.call(shapes, function(shape){
  shape.addEventListener("click", function(){
    var colorNum = Math.floor(Math.random() * colors.length);
    shape.style['background-color'] = colors[colorNum];
  });
});
&#13;
.shape{
  display: inline-block;
  width: 50px;
  height: 50px;
  margin: 10px;
  border-radius: 5px;
  background-color: gray;
  cursor: pointer;
}
&#13;
<h3>Click on the shapes to change their color</h3>

<div class="shape"></div>
<div class="shape"></div>
<div class="shape"></div>
<div class="shape"></div>
<div class="shape"></div>
<div class="shape"></div>
<div class="shape"></div>
&#13;
&#13;
&#13;

使用您的代码。

&#13;
&#13;
// Just add all the possible colours in this array.
var colors = ["blue", "yellow", "green", "purple", "rgb(250, 175, 72)"];
var start = new Date().getTime();

function makeShapeAppear() {
  var top = Math.random() * 400;
  var left = Math.random() * 700;
  var width = (Math.random() * 200) + 100;
  if (Math.random() > 0.5) {
    document.getElementById("shape").style.borderRadius = "50%";
  } else {
    document.getElementById("shape").style.borderRadius = "0"
  }
  document.getElementById("shape").style.backgroundColor = "red";
  document.getElementById("shape").style.width = width + "px";
  document.getElementById("shape").style.height = width + "px";
  document.getElementById("shape").style.top = top + "px";
  document.getElementById("shape").style.left = left + "px";
  document.getElementById("shape").style.display = "block"
  start = new Date().getTime();
}
function appearAfterDelay() {
  setTimeout(makeShapeAppear, Math.random() * 1000);
}

appearAfterDelay()

document.getElementById("shape").onclick = function() {
  var colorNum = Math.floor(Math.random() * colors.length);
  document.getElementById("shape").style.backgroundColor = colors[colorNum];;
  var end = new Date().getTime();
  var timeTaken = (end - start) / 1000;
  document.getElementById("timeTaken").innerHTML = timeTaken + "s"
  appearAfterDelay(); 
}
&#13;
#shape {
  width: 200px;
  height: 200px;
  background-color: red;
  display: none;
  position: relative;
}
body {
  font-family: sans-serif;
}
h1 {
  position: relative;
  top: -20px;
}
p {
  position: relative;
  top: -30px;
}
#yourTime {
  font-weight: bold;
}
&#13;
<h1 id="h1">Test Your Reactions!</h1>
<p>Click on the square or circle as soon as it appears!</p>
<p id="yourTime"> Your time: <span id="timeTaken"></span></p>
<div id="shape"></div>
&#13;
&#13;
&#13;