-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxes.html
91 lines (75 loc) · 2.21 KB
/
boxes.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
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: center;
padding: 10px;
}
#playground {
border: 1px solid black;
min-height: 300px;
margin-top: 20px;
padding: 10px;
display: flex;
flex-wrap: wrap;
}
.magic-box {
background-color: gray;
width: 200px;
height: 200px;
margin: 10px;
border: 2px solid white;
transition: border-radius ease .1s;
}
</style>
<script>
function changeColor(color) {
if (selected) {
selected.style.backgroundColor = color;
}
}
var selected;
function select(element) {
if (selected) {
selected.style.borderColor = 'white';
}
element.style.borderColor = 'black';
selected = element;
}
function makeCircle() {
if (selected) {
selected.style.borderRadius = '50%';
}
}
function makeSquare() {
if (selected) {
selected.style.borderRadius = '0';
}
}
function addBox(containerId) {
let newBox = document.createElement("div");
newBox.className = "magic-box";
newBox.onclick = function() {select(this)};
document.getElementById(containerId).appendChild(newBox);
}
function removeBox(containerId) {
if (selected) {
document.getElementById(containerId).removeChild(selected);
}
}
</script>
</head>
<body>
<button onclick="changeColor('green')">Green</button>
<button onclick="changeColor('red')">Red</button>
<button onclick="makeCircle()">Circle</button>
<button onclick="makeSquare()">Square</button>
<button onclick="addBox('playground')">Add box</button>
<button onclick="removeBox('playground')">Remove box</button>
<div id="playground">
<div class="magic-box" onclick="select(this)"></div>
<div class="magic-box" onclick="select(this)"></div>
</div>
</body>
</html>