简体   繁体   English

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

[英]How do I make shapes change color after clicked on using Javascript?

I'm working on a project and am trying to create a simple reaction game.我正在做一个项目,正在尝试创建一个简单的反应游戏。

To make it more interesting I want the shapes to change color after being clicked on.为了让它更有趣,我希望形状在被点击后改变颜色。 I researched multiple solution to this problem, but all of them require the page to be refreshed.我研究了这个问题的多种解决方案,但所有这些都需要刷新页面。 I also don't understand how to attach it to a specific shape, and not to the background-color of the whole page.我也不明白如何将它附加到特定的形状,而不是整个页面的背景颜色。

Here is my code...这是我的代码...

<!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>

As you can see the shape is supposed to change its color to a different color each time it's clicked on.如您所见,每次单击该形状时,它的颜色都应该更改为不同的颜色。 Please help me figure out how to change the color to a random one each time.请帮我弄清楚如何每次将颜色更改为随机颜色。

Any help and suggestions are welcomed !欢迎任何帮助和建议! (Please excuse me if I'm not phrasing or asking the question correctly, this is my first post on Stack Overflow, so I will get better with time!) (如果我的措辞或提问不正确,请原谅我,这是我在 Stack Overflow 上的第一篇文章,所以我会随着时间的推移变得更好!)

Random colors can be generated with the following function…可以使用以下函数生成随机颜色……

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

  var color = '#';

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

  return color;
}

You can paste it in after your script declaration and start variable…您可以在脚本声明和启动变量之后粘贴它...

<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;
}

You will then have to exchange “red” with getRandomColor()然后你将不得不用getRandomColor()交换“red”

document.getElementById("shape").style.backgroundColor = getRandomColor();

If you have any other question please ask!如有其他问题请追问!

The background-color may be the background of the shape, it is not necessarily the background of the page. background-color可能是形状的背景,不一定是页面的背景。 This is usually the style attribute you use to make a div change color.这通常是您用来使 div 更改颜色的样式属性。

 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]; }); });
 .shape{ display: inline-block; width: 50px; height: 50px; margin: 10px; border-radius: 5px; background-color: gray; cursor: pointer; }
 <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>

With your code.用你的代码。

 // 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(); }
 #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; }
 <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何让我的分区在点击时改变颜色? - How do I make my divisions to change color when they are clicked? 使用 Javascript 单击正方形时,如何使它们的颜色发生变化? - How would I make the color of squares change when they are clicked using Javascript? 如何使 div 元素移动到随机 position 并在使用 Javascript 事件单击时获得随机颜色? - How do I make a div-element move to a random position and also get random color when it is clicked using Javascript events? 我需要使用 HTML、CSS 和 Javascript 制作一个按钮,以便在单击按钮时将字体颜色从红色变为红色 - I need to make a button using HTML, CSS, and Javascript to change font color from to red when the button is clicked 如何更改按钮的颜色,以便在使用jQuery单击按钮时将其更改为另一种颜色? - How do I change the color of a button so that it changes to another color when its clicked using jQuery? 如何更改我单击的特定按钮的此按钮背景颜色? 使用 javascript - How can I change this button background color of the specific button that i have clicked? using javascript 单击链接时如何更改跨度颜色 - How can I make a span change color when a link is clicked 如何使用 javascript 在 canvas 中使颜色不可见 - How do I make a color invisible in a canvas using javascript 如何使用javascript更改背景颜色? - How do I change the background color using javascript? 如何使用JavaScript更改背景颜色 - How do i change the background color using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM