-
Notifications
You must be signed in to change notification settings - Fork 0
/
0810-14-circle-toggle.html
108 lines (98 loc) · 2.8 KB
/
0810-14-circle-toggle.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width, initial-scale=1.0">
<title>環狀排列 2</title>
<style>
.rect {
position: relative;
width: 800px;
height: 700px;
border: 1px solid gray;
background-color: darkcyan;
}
.ball {
position: absolute;
width: 50px;
height: 50px;
color: rgb(0, 0, 0);
background-color: rgb(131, 251, 255);
border: 1px solid olivedrab;
border-radius: 50%;
text-align: center;
line-height: 50px;
transition: 400ms;
}
.big-ball {
position: absolute;
width: 80px;
height: 80px;
color: white;
background-color: rgb(0, 64, 255);
border-radius: 50%;
text-align: center;
line-height: 80px;
left: 360px;
top: 260px;
border: 1px solid black;
cursor: pointer;
}
</style>
</head>
<body>
<div class="rect">
<div class="big-ball">
<!-- click -->
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
const rect = $('.rect');
const bigBall = $('.big-ball');
let isOpened = false; // 一開始是收合的狀態
const ballNum = 12;
const angUnit = Math.PI*2/ballNum;
const balls = [];
for(let i=0; i<ballNum; i++){
const b = $(`<div class="ball">${i+1}</div>`); // 建立元素
b.css({
left: 400 -25 ,
top: 300 -25
});
balls.push({
el: b,
bx: 400 -25,
by: 300 -25,
ex: 400 -25 + 250 * Math.cos(i*angUnit),
ey: 300 -25 + 250 * Math.sin(i*angUnit),
});
rect.append(b);
}
rect.append(bigBall);
bigBall.on('click', function(){
isOpened = !isOpened;
balls.forEach(function(obj, i){
let tx, ty;
if(isOpened){
tx = obj.ex;
ty = obj.ey;
} else {
tx = obj.bx;
ty = obj.by;
}
setTimeout(function(){
obj.el.css({
left: tx,
top: ty,
})
}, 100*i);
});
})
//cos X軸
//sin Y軸
//https://zh.javascript.info/settimeout-setinterval
</script>
</body>
</html>