-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.html
191 lines (189 loc) · 4.4 KB
/
touch.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{ margin: 0; padding: 0; }
.box {
margin: 50px auto 0 auto;
width: 400px;
height: auto;
border: solid 1px #222;
}
#cont {
width: 400px;
height: 600px;
position: relative;
overflow: hidden;
}
#go {
width: 100%;
height: 600px;
position: absolute;
top: 0;
font: 700 60px '微软雅黑';
text-align: center;
z-index: 99;
}
#go span {
cursor: pointer;
background-color: #fff;
border-bottom: solid 1px #222;
}
#main {
width: 400px;
height: 600px;
position: relative;
top: -150px;
}
.row {
width: 400px;
height: 150px;
}
.row div {
width: 99px;
height: 149px;
border: solid 1px #222;
float: left;
border-top-width: 0;
border-left-width: 0;
cursor: pointer;
}
#count {
border-top: solid 1px #222;
width: 400px;
height: 50px;
font: 700 36px/50px '微软雅黑';
text-align: center;
}
</style>
</head>
<body>
<div class="box">
<!-- 布局 -->
<div id="cont">
<div id="go">
<span>Game start</span>
</div>
<div id="main">
</div>
</div>
<div id="count"></div>
</div>
</body>
<script>
//得当前样式
function getStyle(obj,arrt){
//兼容IE
return obj.currentStyle ? obj.currentStyle[arrt] : getComputedStyle(obj,null)[arrt];
}
var main = document.getElementById('main');
var go = document.getElementById('go');
var count = document.getElementById('count');
var cols = ['#1AAB8A','#E15650','#121B39','#80A84E'];
//动态创建div
function cDiv(classname){
//创建div
var oDiv = document.createElement('div');
//随机值
var index = Math.floor(Math.random()*4);
//行 添加相应的class类
oDiv.className = classname;
//创建行之后再动态创建4个小div并添加到行里面
for(var j =0;j<4;j++){
var iDiv = document.createElement('div');
oDiv.appendChild(iDiv);
}
//然后把行添加到main里面
//判断需要添加的位置
if(main.children.length == 0){
main.appendChild(oDiv);
}else {
main.insertBefore(oDiv, main.children[0]);
}
//随机给行里面的某一个div添加背景色
oDiv.children[index].style.backgroundColor = cols[index];
//标记颜色盒子
oDiv.children[index].className = "i";
}
//移动div
function move(obj){
//关闭上一个定时器
clearInterval(obj.timer);
//默认速度与计分
var speed = 5,num = 0;
//定时器管理与开启定时器
obj.timer = setInterval(function(){
//速度
var step = parseInt(getStyle(obj,'top')) + speed;
//移动盒子
obj.style.top = step + 'px';
//判断并创建新的盒子
if(parseInt(getStyle(obj,'top')) >= 0){
cDiv('row');
obj.style.top = -150 + 'px';
}
//删除边界外的盒子
if(obj.children.length == 6){
//删除前,如果有盒子没有点击则结束游戏
for(var i = 0;i<4;i++){
if(obj.children[obj.children.length - 1].children[i].className == 'i'){
//游戏结束
obj.style.top = '-150px';
count.innerHTML = '游戏结束,最高得分: ' + num;
//关闭定时器
clearInterval(obj.timer);
//显示开始游戏
go.children[0].innerHTML = 'Renew game';
go.style.display = "block";
}
}
obj.removeChild(obj.children[obj.children.length - 1]);
}
//点击与计分
obj.onclick = function(event){
//点击的不是白盒子
// 兼容IE
event = event || window.event;
if((event.target? event.target : event.srcElement).className == 'i'){
//点击后的盒子颜色
(event.target? event.target : event.srcElement).style.backgroundColor = "#bbb";
//清除盒子标记
(event.target? event.target : event.srcElement).className = '';
//计分
num++;
//显示得分
count.innerHTML = '当前得分: ' + num;
}else {
//游戏结束
obj.style.top = 0;
count.innerHTML = '游戏结束,最高得分: ' + num;
//关闭定时器
clearInterval(obj.timer);
//显示开始游戏
go.children[0].innerHTML = 'you lost';
go.style.display = "block";
}
//盒子加速
if(num%20 == 0){
speed++;
}
}
},20)
}
//开始游戏
go.children[0].onclick = function(){
//开始前判断main里面是否有盒子,有则全部删除
if(main.children.length){
//暴力清楚main里面所有盒子
main.innerHTML = '';
}
//清空计分
count.innerHTML = '点击开始';
//隐藏开始盒子
this.parentNode.style.display = "none";
//调用定时器
move(main);
}
</script>
</html>