-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.html
95 lines (83 loc) · 2.26 KB
/
colors.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.blocks {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.item {
width: 50px;
height: 50px;
border: 1px solid black;
margin: 10px;
display: flex;
justify-content: center;
align-items: center;
}
</style>
<body>
<div class="blocks-coloring-container">
<div class="controls">
<input type="number" min="0" class="quantity-selector" />
<input type="color" class="color-selector" />
</div>
<div class="blocks"></div>
</div>
<!-- <script src="blocks"></script> -->
<script>
const quantitySelector = document.querySelector(".quantity-selector");
const colorSelector = document.querySelector(".color-selector");
const blocksContainer = document.querySelector(".blocks");
const createMarkup = quantity => {
let markup = "";
for (let i = 0; i < quantity; i++) {
const block = document.createElement("div");
block.className = "item";
block.innerText = i + 1;
markup += block.outerHTML;
}
return markup;
}
quantitySelector.addEventListener("change", e => {
const quantity = e.target.value;
const markup = createMarkup(quantity);
blocksContainer.innerHTML = markup;
});
const colorTheBlock = (block, color) => {
block.style.backgroundColor = color;
};
let changed = false;
colorSelector.addEventListener("change", e => {
const color = e.target.value;
const items = blocksContainer.querySelectorAll(".item");
changed = !changed;
for (let i = 0; i < items.length; i++) {
const currentBlock = items[i];
const blockNumber = i + 1;
if (changed) {
if (blockNumber % 2 != 0) {
colorTheBlock(currentBlock, color);
}
else {
colorTheBlock(currentBlock, "#ffffff");
}
}
else {
if (blockNumber % 2 == 0) {
colorTheBlock(currentBlock, color);
}
else {
colorTheBlock(currentBlock, "#ffffff");
}
}
}
});
</script>
</body>
</html>