-
Notifications
You must be signed in to change notification settings - Fork 0
/
balloon.html
132 lines (115 loc) · 2.94 KB
/
balloon.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1, maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
html,
body {
height: 100vh;
width: 100vw;
overflow: hidden;
}
* {
padding: 0;
margin: 0;
-webkit-tap-highlight-color: transparent;
}
.ball {
width: 15vmin;
height: 15vmin;
border-radius: 50%;
/* background-color: #6fcac4; */
border-bottom-right-radius: 3vmin;
transform: rotate(45deg);
top: 110vh;
left: 0;
position: absolute;
box-shadow: -8px -8px 80px -8px #6fcac4 inset;
}
.ball:hover {
cursor: pointer;
}
.ball::after {
position: absolute;
left: 14vmin;
bottom: 0px;
content: '';
width: 1vmin;
height: 1vmin;
background-color: #6fcac4;
border-radius: 4px;
}
</style>
</head>
<body>
4
</body>
<script>
var count = 0;
// 1.每隔1s生成10个气球
createDOM();
// 2.每隔气球往上,速度不一样
move();
// 3.点击气球消失
clickDiv();
// 4.隔1s,移除超过屏幕的div
removeBall();
function removeBall() {
var divs = document.querySelectorAll('div');
// 删除超过屏幕范围的气球
for (let i = 0; i < divs.length; i++) {
var div = divs[i];
if (div.offsetTop < -150) {
document.body.removeChild(div);
}
}
setTimeout(removeBall, 100);
}
function createDOM() {
var divs = document.querySelectorAll('div');
if (divs.length < 20) {
// 生成div判断下,防止同屏的气球过多;
var tmpDom = document.createDocumentFragment();
for (let index = 0; index < 5; index++) {
var div = document.createElement('div');
div.style.left = Math.random() * 85 + 'vw';
div.speed = Math.ceil(Math.random() * 5);
div.className = "ball";
tmpDom.appendChild(div);
}
document.body.appendChild(tmpDom);
}
setTimeout(createDOM, 2000);
}
function move() {
var balls = document.querySelectorAll('.ball');
for (let index = 0; index < balls.length; index++) {
var tmpBall = balls[index];
var top = tmpBall.offsetTop;
tmpBall.style.top = (top - tmpBall.speed) + 'px';
}
setTimeout(move, 16);
}
function clickDiv() {
document.body.onclick = function (event) {
if (event.target.nodeName === 'DIV') {
if (count === 10) {
}
++count;
document.body
var tmpTarget = event.target.style;
tmpTarget.transition = 'all 0.5s';
tmpTarget.transform = 'scale(0.1)'; // 此处不能等于设为0,有点意外...
setTimeout(() => {
document.body.removeChild(event.target);
}, 500);
}
}
}
// 定时器和事件代理
</script>
</html>