-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayball.html
133 lines (130 loc) · 4.92 KB
/
playball.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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>球球来回弹</title>
</head>
<style type="text/css">
body{position:relative;}
.box{width:200px;height:200px;border:1px solid black;position:absolute;top:100px;left:40px;}
.box .ball{background:red;width:20px;height:20px;border-radius:50%;position:absolute;left:100px;top:10px;display:none;}
.boxtwo{width:400px;height:400px;border:1px solid black;position:absolute;top:100px;left:340px;}
.boxtwo .balltwo{background:red;width:20px;height:20px;border-radius:50%;position:absolute;left:100px;top:0px;display:none;}
</style>
<body>
</body>
<script>
// 1.产生随机数,让球来回滚动
function Balloon(){
var that = this;
that.box = document.createElement("div");
that.box.className = "box";
that.ball = document.createElement("div");
that.ball.className = "ball";
document.body.appendChild(that.box);
that.box.appendChild(that.ball);
that.move();
}
Balloon.prototype.move = function(){
var that = this;
var randomx = parseInt(Math.random() * (parseInt(getComputedStyle(that.box,false)["width"]) - parseInt(getComputedStyle(that.ball,false)["width"])));
var randomy = parseInt(Math.random() * (parseInt(getComputedStyle(that.box,false)["width"]) - parseInt(getComputedStyle(that.ball,false)["height"])));
that.ball.style.display = "block";
var oleft = parseInt(getComputedStyle(that.ball,false).left);
var otop = parseInt(getComputedStyle(that.ball,false).top);
that.timer = setInterval(function(){
if(oleft < randomx){
oleft += 1;
}else if(oleft > randomx){
oleft -= 1;
}else if (oleft == randomx){
randomx = parseInt(Math.random() * (parseInt(getComputedStyle(that.box,false)["width"]) - parseInt(getComputedStyle(that.ball,false)["width"]))) + 1;
};
if(otop < randomy){
otop += 1;
}else if(otop > randomy){
otop -= 1;
}else if (otop == randomy){
randomy = parseInt(Math.random() * (parseInt(getComputedStyle(that.box,false)["width"]) - parseInt(getComputedStyle(that.ball,false)["height"]))) + 1;
};
that.ball.style.left = oleft + "px";
that.ball.style.top = otop + "px";
},30)
}
var running = new Balloon();
// 2.球撞到盒子边缘产生随机数,紧贴边缘;
function Balltwo(){
var that = this;
that.box = document.createElement("div");
that.box.className = "boxtwo";
that.ball = document.createElement("div");
that.ball.className = "balltwo";
document.body.appendChild(this.box);
this.box.appendChild(this.ball);
that.move();
}
Balltwo.prototype.move = function(){
var that = this;
that.ball.style.left = parseInt(Math.random() * 300) + "px";
that.ball.style.top = parseInt(Math.random() * 300) + "px";
that.top = parseInt(that.ball.style.top);
that.left = parseInt(that.ball.style.left);
that.ball.style.display = "block";
that.lbl = parseInt(Math.random() * 2)==1?true:false;
that.tbl = parseInt(Math.random() * 2)==1?true:false;
that.w = that.box.offsetWidth - that.ball.offsetWidth;
that.h = that.box.offsetHeight - that.ball.offsetHeight;
that.timer = setInterval(function(){
if(that.lbl == true){
if(that.left < that.w) {
that.left+=2;
}else{
that.lbl = false;
}
}else{
if(that.left > 0){
that.left-=2;
}else{
that.lbl = true;
}
}
if(that.tbl == true){
if(that.top < that.h) {
that.top+=2;
}else{
that.tbl = false;
}
}else{
if(that.top > 0){
that.top-=2;
}else{
that.tbl = true;
}
}
that.ball.style.top = that.top + "px";
that.ball.style.left = that.left + "px";
},10);
};
function Ballthr(){
this.ball.style.background = "green";
}
function Ballfour(){
this.ball.style.background = "blue";
}
function Ballfive(){
this.ball.style.background = "greenyellow";
}
function Ballsix(){
this.ball.style.background = "violet";
}
Ballthr.prototype = new Balltwo();
Ballfour.prototype = new Balltwo();
Ballfive.prototype = new Balltwo();
Ballsix.prototype = new Balltwo();
var run1 = new Balltwo();
var run2 = new Ballthr();
var run3 = new Ballfour();
var run4 = new Ballfive();
var run5 = new Ballsix();
</script>
</html>