原型方法 烟花

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>

	<style>
		.container{
			width: 80%;
			height: 600px;
			border: 2px solid red;
			background: #000;
			margin:20px auto;
			cursor: pointer;
			position: relative;
			left: 0;
			top: 0;
			overflow: hidden;
		}
		.fire{
			width: 10px;
			height:10px;
			position: absolute;
			bottom: 0;
		}
		.small-fire{
			width: 10px;
			height: 10px;
			position: absolute;
			border-radius: 50%;
		}
	</style>

</head>
<body>
	
	<div class="container">
		
	</div>

</body>
</html>	
<script src="../../animate10.0.js"></script>
<script src="../../public.js"></script>
<script>
	//在container中点击
	//创建一个烟花在container中,
	//烟花向上移动
	//移动到鼠标点击的位置后爆炸
	var container=document.getElementsByClassName("container")[0];
	container.onclick=function(e){
		var e=e||event;
		var x=e.offsetX;
		var y=e.offsetY;
		new FireWork(x,y);
	}
	function FireWork(x,y){
		this.x=x;
		this.y=y;
		this.init();
	}
	
	FireWork.prototype.init=function(){
		this.createFire();
	}
	FireWork.prototype.createFire=function(){
	this.fire=document.createElement("div");
	container.appendChild(this.fire);
	this.fire.className="fire";
	this.fire.style.left=this.x-this.fire.offsetWidth/2+"px";
	this.fire.style.background="red";
	this.fireMove();
	}
	
	FireWork.prototype.fireMove=function(){
		this.fireTimer=setInterval(function(){
			this.fire.style.top=this.fire.offsetTop-5+"px";
			if(this.fire.offsetTop<=this.y){
				clearInterval(this.fireTimer);
				this.fire.remove();
				this.fireBomb();
			}
		}.bind(this),10);
	}
	FireWork.prototype.fireBomb=function(){
		var r=getRand(50,200);
		for(var i=0;i<20;i++){
			var div=document.createElement("div");
			div.className="small-fire";
			container.appendChild(div);
			div.style.left=this.x-div.offsetWidth/2+"px";
			div.style.top=this.y+"px";
			div.style.background=getRandColor();
			this.bomb(div,r,i);
		}
	}
	
	FireWork.prototype.bomb=function(div,r,i){
		var l=this.x+Math.round(Math.cos(i*18*Math.PI/180)*r);
		var t=this.y-Math.round(Math.sin(i*18*Math.PI/180)*r);
		animate(div,{
			left:l,
			top:t
		},function(){
			div.remove();
		})
	}
</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值