自动放烟花 (对象+原型+构造函数)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>烟花+原型+构造函数</title>
</head>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
background: black;
overflow: hidden;
}
#auto{
width: 150px;
height: 50px;
background: green;
color: #fff;
font-size: 16px;
line-height: 50px;
text-align: center;
position: absolute;
left:50%;
top:20px;
margin-left:-75px;
border-radius: 10px;
cursor: pointer;
}
.fire{
width: 3px;
height:30px;
position: absolute;
overflow: hidden;
}
.spark{
width: 3px;
height: 3px;
position: absolute;
overflow: hidden;
border-radius: 50%;
}
</style>
<script>
window.onload = function (){
var oBox = document.getElementById('auto');
var timer ;
oBox.onclick = function(e){
e = e || window.event;
e.stopPropagation ? e.stopPropagation():e.canceBubble = true;
if(timer){
clearInterval(timer);
timer ='';
}else{
timer = setInterval(function(){
var x =parseInt( Math.random() * document.documentElement.clientWidth);
var y = parseInt(Math.random() * document.documentElement.clientHeight);
var fire = new Fire(x,y);
fire.init();
fire.lanuch();
fire.boom();
},1000);
}
}
document.onclick = function(e){
e = e || window.event;
var fire = new Fire(e.clientX,e.clientY);
fire.init().lanuch();
}
}
function Fire (x,y){
this.x = x;
this.y = y;
this.ele = document.createElement('div');
}
Fire.prototype.init = function(){
this.ele.className = 'fire';
this.ele.style.left = this.x +'px' ;
this.ele.style.top = document.documentElement.clientHeight-30 +'px';
this.ele.style.background = randomColor();
document.body.appendChild(this.ele);
return this;
}
Fire.prototype.lanuch = function(){
var self = this;
upgradeStartMove(self.ele,{top:self.y,height:3},function(){
document.body.removeChild(self.ele);
var temp = parseInt(Math.random()*30)+30;
for(var i = 0 ;i < temp;i++){
self.boom();
}
});
}
Fire.prototype.boom = function(){
var spark = new Spark(this.x,this.y);
spark.init().fly();
}
function Spark(x,y){
this.ele = document.createElement('div');
this.x = x;
this.y = y;
}
Spark.prototype.init = function(){
this.ele.className = 'spark';
this.ele.style.left = this.x + 'px';
this.ele.style.top = this.y +'px';
this.ele.style.background = randomColor();
document.body.appendChild(this.ele);
return this;
}
Spark.prototype.fly = function(){
var self = this;
var xspeed = parseInt(Math.random() * 20 * (Math.random() > 0.5 ? 1 : -1));
var yspeed = parseInt(Math.random() * 20 *(Math.random() > 0.5 ? 1 : -1));
var timer = setInterval(function(){
yspeed++;
self.ele.style.left = self.ele.offsetLeft + xspeed +'px';
self.ele.style.top = self.ele.offsetTop + yspeed + 'px';
if( (self.ele.offsetLeft < 0) || (self.ele.offsetLeft > document.documentElement.clientX ) ||( self.ele.offsetTop > document.documentElement.clientHeight)){
clearInterval(timer);
document.body.removeChild(self.ele);
}
},30);
}
function randomColor(){
rcolor = parseInt(Math.random()*256);
gcolor = parseInt(Math.random()*256);
bcolor = parseInt(Math.random()*256);
var str = 'rgb('+rcolor+','+gcolor+','+bcolor+')';
return str;
}
function upgradeStartMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var bStop = true;
for(var attr in json){
var current;
if(attr == 'opacity'){
current = parseFloat(getStyle(obj,attr)*100);
current = parseInt(Math.round(current));
}else{
current = parseInt(getStyle(obj,attr));
}
var speed = (json[attr] - current) / 8;
speed = speed > 0? Math.ceil(speed):Math.floor(speed);
if (attr == 'opacity'){
obj.style[attr] = (current+speed)/100;
obj.style.filter = '(opacity='+(current+speed)+')';
}else{
obj.style[attr] = current+speed+'px';
}
if(current != json[attr]){
bStop = false;
}
}
if(bStop){
clearInterval(obj.timer);
if(fn){
fn();
}
}
},30);
}
function getStyle(ele,attr){
var res;
if(window.getComputedStyle){
res = getComputedStyle(ele)[attr];
}else if(ele.currentStyle){
res = ele.currentStyle[attr];
}else{
res = ele.style[attr];
}
return res;
}
</script>
<body>
<div id="auto">自动播放</div>
</body>
</html>