面向对象系列讲解——碰壁反弹-面向对象版本
HTML5学堂:面向对象系列讲解——碰壁反弹-面向对象版本。之前大家可能有写过碰壁反弹效果,如果生成多个球的话会感觉有点卡顿。本文章要给大家讲解用面向对象来实现碰壁反弹,不仅仅减少了代码量,而且还提升了性能。
多个球的碰壁反弹效果如下:
用面向对象实现碰壁反弹的基本功能
需要的属性:
父级对象
本身的对象
控制移动的属性
计时器属性
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
// 防止指向错误
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
this.posX = 0;
-
this.posY = 0;
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
// 运动函数
-
Bounce.prototype.move = function() {
-
this.posX++;
-
this.posY++;
-
-
// CSS赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
// 实例化
-
var bounce = new Bounce("outer", "inner");
-
</script>
-
</body>
-
</html>
实现有边界的碰壁反弹
需要的属性:
方向
边界
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - HTML5学堂</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
// 修改初始位置为随机产生
-
this.posX = 100;
-
this.posY = 100;
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
-
// 初始化
-
this.initial();
-
}
-
-
/*
-
* [initial 初始化位置]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.initial = function() {
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX++;
-
if (this.posX >= this.limitX) {
-
this.moveX = false;
-
};
-
} else {
-
this.posX--;
-
if (this.posX <= 0) {
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY++;
-
if (this.posY >= this.limitY) {
-
this.moveY = false;
-
};
-
} else {
-
this.posY--;
-
if (this.posY <= 0) {
-
this.moveY = true;
-
};
-
}
-
-
// CSS赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
var bounce = new Bounce("outer", "inner");
-
</script>
-
</body>
-
</html>
欢迎沟通交流~HTML5学堂
提取速度属性的碰壁反弹
需要的属性:
速度属性
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
// 修改初始位置为随机产生
-
this.posX = 100;
-
this.posY = 100;
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
-
// 初始化
-
this.initial();
-
}
-
-
/*
-
* [initial 初始化位置]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.initial = function() {
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
-
// CSS赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
var bounce = new Bounce("outer", "inner");
-
</script>
-
</body>
-
</html>
实现颜色随机的碰壁反弹
需要的属性:
颜色属性
重写初始化方法
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
// 修改初始位置为随机产生
-
this.posX = 100;
-
this.posY = 100;
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.moveY = true;
-
};
-
}
-
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
// 随机颜色的球球
-
function RanPosBall(parentId, ballId) {
-
// 继承属性
-
Bounce.call(this, parentId, ballId);
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
var ranPosBall = new RanPosBall("outer", "inner");
-
-
</script>
-
</body>
-
</html>
欢迎沟通交流~HTML5学堂
实现随机速度的碰壁反弹
重写父类的速度属性
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
// 修改初始位置为随机产生
-
this.posX = 100;
-
this.posY = 100;
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
// var bounce = new Bounce("outer", "inner");
-
-
// 随机颜色的球球
-
function RanPosBall(parentId, ballId) {
-
// 继承属性
-
Bounce.call(this, parentId, ballId);
-
// 增加三种颜色属性
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
-
// 重写速度属性
-
this.speed = Math.floor(Math.random() * 5 + 1);
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
var ranPosBall = new RanPosBall("outer", "inner");
-
-
-
</script>
-
</body>
-
</html>
实现随机方向的碰壁反弹
重写方向属性
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
<div class="ball" id="inner"></div>
-
</div>
-
<script>
-
function Bounce(parentId, ballId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.getElementById(ballId);
-
// 修改初始位置为随机产生
-
this.posX = 100;
-
this.posY = 100;
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
};
-
-
// var bounce = new Bounce("outer", "inner");
-
-
// 随机颜色的球球
-
function RanPosBall(parentId, ballId) {
-
// 继承属性
-
Bounce.call(this, parentId, ballId);
-
// 增加三种颜色属性
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
// 重写速度属性
-
this.speed = Math.floor(Math.random() * 5 + 1);
-
this.moveX = Math.floor(Math.random() * 2);
-
this.moveY = Math.floor(Math.random() * 2);
-
if (this.moveX == 1) {
-
this.moveX = true;
-
} else {
-
this.moveX = false;
-
}
-
if (this.moveY == 1) {
-
this.moveY = true;
-
} else {
-
this.moveY = false;
-
}
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
var ranPosBall = new RanPosBall("outer", "inner");
-
-
-
</script>
-
</body>
-
</html>
欢迎沟通交流~HTML5学堂
生成多个球球的碰壁反弹
创建球球插入到父级上
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
-
</div>
-
<script>
-
function Bounce(parentId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.createElement("div");
-
this.outer.appendChild(this.inner);
-
// 修改初始位置为随机产生
-
this.posX = Math.ceil(Math.random() * 550);
-
this.posY = Math.ceil(Math.random() * 350);
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
this.evaluatePos();
-
};
-
-
Bounce.prototype.evaluatePos = function() {
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
}
-
-
// var bounce = new Bounce("outer", "inner");
-
-
// 随机颜色的球球
-
function RanPosBall(parentId, ballId) {
-
// 继承属性
-
Bounce.call(this, parentId);
-
// 增加三种颜色属性
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
// 重写速度属性
-
this.speed = Math.floor(Math.random() * 5 + 1);
-
this.moveX = Math.floor(Math.random() * 2);
-
this.moveY = Math.floor(Math.random() * 2);
-
if (this.moveX == 1) {
-
this.moveX = true;
-
} else {
-
this.moveX = false;
-
}
-
if (this.moveY == 1) {
-
this.moveY = true;
-
} else {
-
this.moveY = false;
-
}
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
for (var i = 0; i < 1000; i++) {
-
var ranPosBall = new RanPosBall("outer", "inner");
-
};
-
-
-
</script>
-
</body>
-
</html>
代码优化
提取样式赋值
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 50px;
-
height: 50px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
-
</div>
-
<script>
-
function Bounce(parentId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.createElement("div");
-
this.outer.appendChild(this.inner);
-
// 修改初始位置为随机产生
-
this.posX = Math.ceil(Math.random() * 550);
-
this.posY = Math.ceil(Math.random() * 350);
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
this.evaluatePos();
-
};
-
-
Bounce.prototype.evaluatePos = function() {
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
}
-
-
// var bounce = new Bounce("outer", "inner");
-
-
// 随机颜色的球球
-
function RanPosBall(parentId, ballId) {
-
// 继承属性
-
Bounce.call(this, parentId);
-
// 增加三种颜色属性
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
// 重写速度属性
-
this.speed = Math.floor(Math.random() * 5 + 1);
-
this.moveX = Math.floor(Math.random() * 2);
-
this.moveY = Math.floor(Math.random() * 2);
-
if (this.moveX == 1) {
-
this.moveX = true;
-
} else {
-
this.moveX = false;
-
}
-
if (this.moveY == 1) {
-
this.moveY = true;
-
} else {
-
this.moveY = false;
-
}
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
for (var i = 0; i < 1000; i++) {
-
var ranPosBall = new RanPosBall("outer", "inner");
-
};
-
-
-
</script>
-
</body>
-
</html>
使用CSS3平移控制的碰壁反弹
重写evaluatePos方法
-
<!doctype html>
-
<html>
-
<head>
-
<meta charset="UTF-8">
-
<title>HTML5Course - 梦幻雪冰</title>
-
<link rel="stylesheet" href="css/reset.css">
-
<style>
-
.box {
-
position: relative;
-
width: 600px;
-
height: 400px;
-
margin: 30px auto;
-
border: 1px solid #fcf;
-
}
-
.box div {
-
position: absolute;
-
width: 10px;
-
height: 10px;
-
background-color: #eee;
-
border-radius: 50%;
-
}
-
</style>
-
</head>
-
<body>
-
<div class="box" id="outer">
-
-
</div>
-
<script>
-
function Bounce(parentId) {
-
var _this = this;
-
-
this.outer = document.getElementById(parentId);
-
this.inner = document.createElement("div");
-
this.outer.appendChild(this.inner);
-
// 修改初始位置为随机产生
-
this.posX = Math.ceil(Math.random() * 550);
-
this.posY = Math.ceil(Math.random() * 350);
-
this.moveX = true;
-
this.moveY = true;
-
this.limitX = this.outer.clientWidth - this.inner.offsetWidth;
-
this.limitY = this.outer.clientHeight - this.inner.offsetHeight;
-
// 添加速度
-
this.speed = 3;
-
-
// 调用运动函数
-
this.timer = setTimeout(function() {
-
_this.move();
-
// 注意指向问题
-
setTimeout(arguments.callee, 16);
-
});
-
}
-
-
/*
-
* [move 运动函数]
-
* @return {[type]} [description]
-
*/
-
Bounce.prototype.move = function() {
-
// 水平方向
-
if (this.moveX) {
-
this.posX += this.speed;
-
if (this.posX >= this.limitX) {
-
this.posX = this.limitX;
-
this.moveX = false;
-
};
-
} else {
-
this.posX -= this.speed;
-
if (this.posX <= 0) {
-
this.posX = 0;
-
this.moveX = true;
-
};
-
}
-
-
// 垂直方向
-
if (this.moveY) {
-
this.posY += this.speed;
-
if (this.posY >= this.limitY) {
-
this.posY = this.limitY;
-
this.moveY = false;
-
};
-
} else {
-
this.posY -= this.speed;
-
if (this.posY <= 0) {
-
this.posY = 0;
-
this.moveY = true;
-
};
-
}
-
this.evaluatePos();
-
};
-
-
Bounce.prototype.evaluatePos = function() {
-
// 赋值
-
this.inner.style.left = this.posX + "px";
-
this.inner.style.top = this.posY + "px";
-
}
-
-
// var bounce = new Bounce("outer", "inner");
-
-
// 随机颜色的球球
-
function RanPosBall(parentId) {
-
// 继承属性
-
Bounce.call(this, parentId);
-
// 增加三种颜色属性
-
this.colorR = Math.floor(Math.random() * 256);
-
this.colorG = Math.floor(Math.random() * 256);
-
this.colorB = Math.floor(Math.random() * 256);
-
// 重写速度属性
-
this.speed = Math.floor(Math.random() * 3 + 1);
-
this.moveX = Math.floor(Math.random() * 2);
-
this.moveY = Math.floor(Math.random() * 2);
-
if (this.moveX == 1) {
-
this.moveX = true;
-
} else {
-
this.moveX = false;
-
}
-
if (this.moveY == 1) {
-
this.moveY = true;
-
} else {
-
this.moveY = false;
-
}
-
-
// 调用重写的初始化方法
-
this.initial();
-
}
-
-
// 继承方法
-
for (var i in Bounce.prototype) {
-
RanPosBall.prototype[i] = Bounce.prototype[i];
-
};
-
-
// 重写初始化的方法
-
RanPosBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
-
function TranslateBall(parentId) {
-
// 属性继承
-
RanPosBall.call(this, parentId);
-
}
-
-
// 方法继承
-
for (var i in RanPosBall.prototype) {
-
TranslateBall.prototype[i] = RanPosBall.prototype[i]
-
};
-
-
// 重写初始化方法
-
TranslateBall.prototype.initial = function() {
-
// 设置背景
-
this.inner.style.backgroundColor = "rgb(" + this.colorR + ", " + this.colorG + ", " + this.colorB + ")";
-
}
-
-
// 重写样式赋值方法
-
TranslateBall.prototype.evaluatePos = function() {
-
this.inner.style.webkitTransform = "translateX(" + this.posX +"px ) translateY(" + this.posY + "px)";
-
}
-
-
for (var i = 0; i < 300; i++) {
-
var translateBall = new TranslateBall("outer");
-
};
-
</script>
-
</body>
-
</html>
欢迎沟通交流~HTML5学堂