HTML实训之烟花效果

在这里插入图片描述
编程过程中使用了"jquery-3.4.1.js这个js包

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			html,
			body {
				overflow: hidden;
			}

			body,
			div,
			p {
				margin: 0;
				padding: 0;
			}

			body {
				background: #000;
				font: 12px/1.5 arial;
				color: #7A7A7A;
			}

			.fire {
				width: 3px;
				height: 3px;
				background: white;
				position: absolute;
			}

			.spark {
				position: absolute;
				width: 6px;
				height: 6px;
			}
			
			.ball {
				background: greenyellow;
				width: 50px; height: 50px;
				position: absolute;
				left :0; top: 300px;
			}
		</style>
		
	</head>
	<body>
		
	</body>
	
	
	<script src="jquery-3.4.1.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		
		$(window).click(function(event){
			//获取鼠标单击位置
			new Fire(event.pageX,event.pageY);
		})
		
		class Fire {
			//需要创建一个烟花元素,并添加到页面上
			constructor(mousex, mousey) {
				this.mousex = mousex;
				this.mousey = mousey;
			    this.ele = $('<div class="fire"></div>');
				this.ele.appendTo("body");
				//修改它的位置
				this.ele.css({
					left: mousex + "px",
					top: $(window).height() + "px"
				})
				
				this.launch();
			}
			launch(){
				this.ele.animate({top: this.mousey}, 500, ()=>{
					//爆炸
					this.explode();
				})
			}
			explode(){
				this.ele.remove();
				//生成若干的彩色的烟花
				
				let count = Math.round(Math.random()*30 + 30); 
				// 30<=count*30+30<61
				// 30=<count<=60
				for(let i=0; i<count; i++){
					new Spark(this.mousex, this.mousey);
				}
			}
		}
		
		class Spark {
			constructor(x,y){
				//需要创建一个烟花元素,并添加到页面上
				this.ele = $('<div class="spark"></div>');
				this.ele.appendTo("body");
				
				//位置
				this.ele.css({
					left : x,
					top : y
				})
				
				//随机颜色
				this.ele.css({
					background: "rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")"
				})
				
				//随机的初始速度
				this.ele.speedx = Math.round(Math.random()*40) - 20;
				this.ele.speedy = Math.round(Math.random()*40) - 20;
				
				//抛物线
				this.parabola()
			}
			parabola(){
				let t = setInterval(()=>{
					this.ele.css({
						left: this.ele.offset().left + this.ele.speedx ,
						top: this.ele.offset().top + this.ele.speedy++
					})
					//当烟花落地时
					if(this.ele.offset().top >= $(window).height()) {
						//关闭定时器
						clearInterval(t);
						//删除烟花
						this.ele.remove();
					}
				}, 30)
			}
		}
	</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值