-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
75 lines (57 loc) · 1.56 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
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Sketch Pad</title>
<link href="css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id = "container">
</div>
<div class= "center">
<button id= "clearButton">CLEAR BOARD</button>
</div>
<script>
//Create the grid
const container = document.querySelector('#container');
let squares = prompt('Choose the number of columns');
createGrid(squares);
function containerWidth(number){
return container.style.width = `${number}px`;
}
function containerHeight(number){
return container.style.height = `${number}px`;
}
function createDiv(){
let div = document.createElement('div');
div.classList.toggle('box')
return container.appendChild(div);
}
function createGrid(cols){
for(var i=1; i <= (cols **2); i++){
createDiv();
}
containerWidth(20 * cols)
containerHeight(20 * cols)
}
//Mouse over changes the color of squares
function random(maxNumber){
return Math.floor(Math.random() * maxNumber);
}
container.addEventListener('mouseover', function(e){
var boxes = e.target;
if (boxes !== container){
boxes.style.backgroundColor = `rgb(${random(255)}, ${random(255)}, ${random(255)})`;
}
})
//Button should ideally clear the board
var clearButton = document.querySelector('#clearButton');
clearButton.addEventListener('click', function(e){
var boxes = document.querySelectorAll('.box');
boxes.forEach(function(box){
box.style.backgroundColor = 'white';
})
})
</script>
</body>
</html>