-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
151 lines (126 loc) · 4.27 KB
/
index.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<html>
<head>
<style>
.container
{
border: 5px solid red;
width: 500px;
height:500px;
position:absolute;
margin:auto;
left:0;
right:0;
}
li
{
list-style:none;
display: inline;
margin:10px;
}
h1{
text-align: center;
}
</style>
</head>
<body>
<h1>Etch-a-Sketch
<div class = "header">
<ul id="gamebuttons">
<li> <button id="btn1" value="Random Color">Random Color</button> </li>
<li> <button id="btn2" value="Change Grid Size">Change Grid Size</button> </li>
<li> <button id="btn3" value="Darken">Darken</button> </li>
<li> <button id="btn4" value="default">Default Style</button> </li>
</ul>
</div>
</h1>
<div class="container">
</div>
</body>
<script>
let i=0;
let container = document.querySelector('.container');
let num = 16;
let backColor = 'black';
const gridSizeBtn = document.querySelector('#btn2');
const colorBtn = document.querySelector('#btn1');
const defaultBtn = document.querySelector('#btn4');
const darkenBtn = document.querySelector('#btn3');
gridSizeBtn.addEventListener('click',(e)=>{
console.log("Size button event");
num=window.prompt("Enter desired of grid");
if(isNaN(num) || num==null || num== "" || num.trim() == "")
{
alert("You didn't enter a number. Please enter number")
}
else
{
while(container.hasChildNodes())
container.removeChild(container.firstChild);
createBox(num);
if(backColor=='black')
changeColor('black');
else
changeRandomColor();
}
});
colorBtn.addEventListener('click',(e)=>{
console.log("Color button event");
changeRandomColor();
});
defaultBtn.addEventListener('click',(e)=>{
console.log("Default button event");
num=16;
while(container.hasChildNodes())
container.removeChild(container.firstChild);
createBox(num);
changeColor('black');
});
darkenBtn.addEventListener('click',(e)=>{
console.log("Darken button event");
darken();
});
function createBox(num)
{
console.log("Grid Size"+num);
for(i=0;i<num*num;i++)
{
const div = document.createElement('div');
const height = 500/num;
div.style.height = `${height}px`;
div.style.width = `${height}px`;
div.style.display = 'inline-block';
container.appendChild(div);
}
}
function changeColor(color)
{
backColor = 'black';
let divs = document.querySelectorAll('.container div');
divs.forEach(div => div.addEventListener('mouseover',(e) =>
{
console.log(e.currentTarget);
e.currentTarget.style.background=color;
}));
}
function changeRandomColor()
{
backColor = 'blue';
let divs = document.querySelectorAll('.container div');
divs.forEach(div => div.addEventListener('mouseover',(e) =>
{
let randomColor = Math.floor(Math.random()*16777215).toString(16);
e.currentTarget.style.background=`#${randomColor}`;
}));
}
function darken()
{
let divs = document.querySelectorAll('.container div');
divs.forEach(div => div.addEventListener('mouseover',(e) =>
{
e.currentTarget.style.opacity = (parseFloat(e.target.style.opacity) || 0) + 0.1;
}));
}
createBox(num);
changeColor(backColor);
</script>
</html>