-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorGame.html
114 lines (110 loc) · 3.61 KB
/
colorGame.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
body{
width: 100%;
height: 100%;
background: #130d06;
margin: 0 auto;
}
.square{
width: 30%;
height: 100px;
margin: 1.6%;
float: left;
background-color: #e2e2e2;
margin: 5px;
border-radius: 10px;
transition: background 0.3s;
}
#container{
margin: 10px auto;
width: 800px;
}
h4{
background: #fff;
text-align: center;
color: black;
margin: 0;
transition: all 0.3s;
}
#title{
color: #fff;
margin: 0px;
display: block;
text-align: center;
text-transform: uppercase;
font-size: 34px;
font-weight: normal;
font-family: comic;
background-color: steelblue;
}
p{
margin: 0 auto;
}
button{
display: inline;
margin: 0px 5px;
border:none;
background: #fff;
font-weight: bold;
padding: 0px;
}
.btnHover{
background: steelblue;
color: white;
}
#message:hover{
cursor: pointer;
}
</style>
</head>
<body>
<h2 id="title"><p>THE <br><span id='headingColor'></span><br>GAME</p></h2>
<h4><span id="message" onclick="reloadFunc()"></span></h4>
<div id="container">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<script type="text/javascript">
var colors=[];
var square=document.querySelectorAll(".square");
var message=document.querySelector("#message");
var heading=document.querySelector("#headingColor");
var title=document.querySelector("#title");
for (var i = 0 ;i < square.length; i++) {
generateRandomColor();
square[i].style.background=colors[i];
square[i].addEventListener("click",function(){
if (displayColor==this.style.background) {
message.textContent="Well Done! Click to reload...";
title.style.background=displayColor;
for (var i = 0 ;i < square.length; i++) {
square[i].style.background=displayColor;
}
}
else{
this.style.background="black";
message.textContent="Nah,Try Again? Click to reload...";
}
});
}
function generateRandomColor() {
var r=Math.floor(Math.random()*255+1);
var g=Math.floor(Math.random()*255+1);
var b=Math.floor(Math.random()*255+1);
colors.push("rgb("+r+", "+g+", "+b+")");
}
var displayColor=colors[Math.floor(Math.random()*square.length)];
heading.textContent=displayColor;
function reloadFunc(){
location.reload();
}
</script>
</body>
</html>