-
Notifications
You must be signed in to change notification settings - Fork 1
/
circle.html
103 lines (94 loc) · 3.34 KB
/
circle.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
<!DOCTYPE html>
<html>
<head>
<!--
nothing important
-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<style>
.layer {
position: absolute;
border: 10px dotted black;
border-radius: 10%;
}
</style>
<script>
window.onload = function() {
document.documentElement.addEventListener('click', handleClick)
}
function handleClick(e) {
var position = {x:e.pageX, y:e.pageY}
if (!isRunning) {
isRunning = true
letsRoll(position)()
}
}
var isRunning = false
// 翻滚起来~
function letsRoll(origin) {
var divs = Array.prototype.slice.call(document.querySelectorAll('.layer'))
var infos = divs.map(div => {
var rect = div.getBoundingClientRect()
var center = {
x: rect.left + rect.width/2,
y: rect.top + rect.height/2
}
var r = Math.sqrt(
Math.pow(center.x - origin.x, 2) + Math.pow(center.y - origin.y, 2)
)
var startAngle = Math.atan2(center.y-origin.y, center.x-origin.x)
return [div, rect, startAngle, r]
})
var step = 0
var func = function() {
infos.forEach(function([div, rect, startAngle, r]) {
move(div, rect, startAngle, r, step, origin)
})
if (step < 360) {
++step
setTimeout(func, 0)
} else {
isRunning = false
}
}
return func
}
/**
* 移动层
* @param div HTMLElement - 要移动的元素
* @param startRect Object - 元素的起始rect
* @param startAngle Number - 元素的起始角度(和原点之间的夹角)
* @param r Number - 半径
* @param step Number - 当前步
* @param origin Object - 原点
*
* @return void
*/
function move(div, startRect, startAngle, r, step, origin) {
var angle = startAngle - step / 360 * 2 * Math.PI
var newCenter = {
x: origin.x + r * Math.cos(angle),
y: origin.y + r * Math.sin(angle)
}
div.style.left = (newCenter.x - startRect.width/2) + 'px'
div.style.top = (newCenter.y - startRect.height/2) + 'px'
div.style.transform = `rotate(${step/360*20}turn)`
}
</script>
</head>
<body onClick='handleClick(event)' style="background: white; width:100%; height:100%">
<div class="layer" style='width:100px; height: 100px; left: 300px; top: 200px; background: red'></div>
<div class="layer" style='width:20px; height: 20px; left: 10px; top: 600px; background: blue'></div>
<div class="layer" style='width:80px; height: 80px; left: 900px; top: 700px; background: green'></div>
<div class="layer" style='width:120px; height: 120px; left: 670px; top: 540px; background: purple'></div>
<div class="layer" style='width: 300px'><a href='https://en.wikipedia.org/wiki/Valar_Morghulis' target='_blank'>Valar Morghulis - Wikipedia</a></div>
<div style='position: fixed; top: 350px; left:400px; z-index: 1; font-size: 300%; border: 5px solid brown; background-color: #EEEEEE'>
<a href='https://baike.baidu.com/item/%E6%B3%B0%E5%B1%B1%E5%B4%A9%E4%BA%8E%E5%89%8D%E8%80%8C%E8%89%B2%E4%B8%8D%E5%8F%98%EF%BC%8C%E9%BA%8B%E9%B9%BF%E5%85%B4%E4%BA%8E%E5%B7%A6%E8%80%8C%E7%9B%AE%E4%B8%8D%E7%9E%AC'
target='_blank'>泰山崩于前而色不变<br/>
麋鹿兴于左而目不瞬</a>
</div>
</body>
</html>