-
Notifications
You must be signed in to change notification settings - Fork 0
/
任务1九宫格.html
123 lines (115 loc) · 2.94 KB
/
任务1九宫格.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
#container{
margin: auto;
position: absolute;
width: 330px;
height: 330px;
top:0;
left:0;
right:0;
bottom:0;
}
.div1{
width:100px;
height:100px;
margin-right:5px;
margin-bottom: 5px;
background: orange;
float: left;
border-radius: 5px;
}
button{
width: 50px;
height: 30px;
background: pink;
border: #fff;
border-radius: 3px;
}
.button{
position: absolute;
left:150px;
bottom: -50px;
}
</style>
</head>
<body>
<div id="container">
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="div1"></div>
<div class="button">
<button id="ch" onclick="ch()">随机</button>
<button id="reset" onclick="fy()">复原</button>
</div>
</div>
<script type="text/javascript">
var num=[]; //随机三个0~8的不重复数字
var con=document.getElementById('container');
var div1=con.getElementsByTagName('div')
var stop;
function getnum(){
for(var i=0;i<3;i++){ //方法有点笨..
switch(i){
case 0: num[0]=Math.floor(Math.random()*9);break;
case 1: num[1]=Math.floor(Math.random()*9);
if(num[1]==num[0]){
i=i-1;
}
break;
case 2: num[2]=Math.floor(Math.random()*9);
if(num[2]==num[0]||num[1]==num[2]){
i=i-1;
}
break;
}
}
//检测随机数字是否符合条件 console.log(num);
}
function getcolor(){ //随机一个颜色
var color=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
var c2='';
for(var i=1;i<7;i++){
var c1= color[Math.floor(Math.random()*16)];
c2+=c1;
}
return '#'+c2
}
//[0]~[8]是变色区域
function ch(){
function ch1(){ //获得三个随机颜色给三个随机不重复的方块
for(var i=0;i<9;i++){
div1[i].style.background='orange'; //每次执行之前先还原颜色
}
getnum()
div1[num[0]].style.background=getcolor();
div1[num[1]].style.background=getcolor();
div1[num[2]].style.background=getcolor();
}
ch1();
stop=setInterval(ch1,1000);
}
function fy(){ //复原9个方块的颜色
clearInterval(stop); //先清除计时器
for(var i=0;i<9;i++){
div1[i].style.background='orange';
}
}
</script>
</body>
</html>
<!--验收标准
1.每次随机生成3个颜色
2.点击按钮一,使3个颜色随机上到九宫格的3个格子上,每秒变化一次
3.点击按钮二,九宫格复原
4.使用javascript原生代码编写-->