-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
97 lines (84 loc) · 2.64 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
<!DOCTYPE html>
<html>
<head>
<title>Etch-a-Sketch!</title>
</head>
<style>
#body{
overflow: hidden;
background-color: coral;
margin: 0px;
}
#header{
font-family: "courier new";
font-size: large;
text-align: center;
}
#clearButton{
height:4vh;
width:10vw;
margin-left:45vw;
}
#mainContainer{
display: grid;
position: absolute;
width:100vw;
}
.gridDiv{
aspect-ratio: 1;
width:1fr;
transition: background-color 0.7s;
}
.gridDiv:hover {
background-color: rgb(161, 59, 59);
}
.gridDivHovered {
background-color: rgb(28, 143, 143);
}
</style>
<body id="body">
<header id="header">Etch-a-Sketch!</header>
<button id="clearButton" onclick="buttonClick()">Clear!</button>
<div id="mainContainer">
</div>
</body>
</html>
<script>
let squaresPerSide=42;
const body = document.querySelector("#body");
let numberOfDivs = (squaresPerSide*squaresPerSide);
buildPage();
function buildPage(){
numberOfDivs = (squaresPerSide*squaresPerSide);
console.log("page building initialized");
var mainContainer = document.querySelector("#mainContainer");
//change cols and rows depending on new squaresperside
mainContainer.style.setProperty('grid-template-columns', 'repeat(' + squaresPerSide + ', 1fr)');
mainContainer.style.setProperty('grid-template-rows', 'repeat(' + squaresPerSide + ', 1fr)');
var toAdd = document.createDocumentFragment();
for(var i=0; i < numberOfDivs; i++){
var newDiv = document.createElement('div');
newDiv.id = 'div'+i;
newDiv.className = 'gridDiv';
toAdd.appendChild(newDiv);
}
mainContainer.appendChild(toAdd);
var squareDivs = document.getElementsByClassName("gridDiv");
var turnBlack = function(obj) {
this.classList.add("gridDivHovered");
};
for (var i = 0; i < squareDivs.length; i++) {
squareDivs[i].addEventListener('mousein', turnBlack, false);
squareDivs[i].addEventListener('mouseleave', turnBlack, false);
}
}
function buttonClick(){
squaresPerSide = prompt("How many squares per side do you want?");
mainContainer = document.querySelector("#mainContainer");
mainContainer.remove();
mainContainer = document.createElement("div");
mainContainer.id="mainContainer";
body.appendChild(mainContainer);
buildPage();
}
</script>