HTML+CSS+JS制作炫酷【烟花特效】

制作炫酷烟花特效

💡本篇内容使用html+css+js制作鼠标点击出现烟花效果,分别介绍了分散型烟花,圆形烟花和爱心形烟花,爱心形烟花算法比较复杂,需要源码的小伙伴可以通过文章末尾链接下载。

一、普通烟花(分散形)

效果展示

请添加图片描述

HTML代码

引入js 文件

<script type="text/javascript" src="buffermove1.js"></script>

CSS代码

创建一个黑色背景

<style type="text/css">
		*{
			padding: 0px;margin: 0px;
		}

		body{
			background: #000;
			width: 100%;
			height:100%;
			overflow: hidden;
		}
	</style>

JS代码

<script type="text/javascript">
  //this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
  function Fireworks(x,y){//x,y鼠标的位置
    this.x=x;
    this.y=y;
    var that=this;
    //1.创建烟花。
    this.ceratefirework=function(){
      this.firework=document.createElement('div');//整个构造函数内部都可以使用
      this.firework.style.cssText=`width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
      document.body.appendChild(this.firework);
      this.fireworkmove();
    };
    //2.烟花运动和消失
    this.fireworkmove=function(){
      buffermove(this.firework,{top:this.y},function(){
        document.body.removeChild(that.firework);//烟花消失,碎片产生
        that.fireworkfragment();
      });
    };
    //3.创建烟花的碎片
    this.fireworkfragment=function(){
      for(var i=0;i<this.ranNum(30,60);i++){
        this.fragment=document.createElement('div');
        this.fragment.style.cssText=`width:5px;height:5px;background:rgb(${this.ranNum(0,255)},${this.ranNum(0,255)},${this.ranNum(0,255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
        document.body.appendChild(this.fragment);
        this.fireworkboom(this.fragment);//将当前创建的碎片传过去,方便运动和删除
      }
    }


    //4.碎片运动
    this.fireworkboom=function(obj){//obj:创建的碎片


      //设点速度(值不同,正负符号不同)
      var speedx=parseInt((Math.random()>0.5?'-':'')+this.ranNum(1,15));
      var speedy=parseInt((Math.random()>0.5?'-':'')+this.ranNum(1,15));


      //初始速度
      var initx=this.x;
      var inity=this.y;
      obj.timer=setInterval(function(){//一个盒子运动
        initx+=speedx;
        inity+=speedy;
        if(inity>=document.documentElement.clientHeight){
          clearInterval(obj.timer);
          document.body.removeChild(obj);
        }
        obj.style.left=initx+'px';
        obj.style.top=inity+'px';
      },20);

    }


    //随机方法
    this.ranNum=function (min,max){
      return Math.round(Math.random()*(max-min))+min;
    };
  }



  document.onclick=function(ev){
    var ev=ev||window.event;
    new Fireworks(ev.clientX,ev.clientY).ceratefirework();
  }
</script>

二、圆形烟花

效果展示
在这里插入图片描述

HTML代码

引入js 文件

<script type="text/javascript" src="buffermove1.js"></script>

CSS代码

创建一个黑色背景

<style type="text/css">
		*{
			padding: 0px;margin: 0px;
		}

		body{
			background: #000;
			width: 100%;
			height:100%;
			overflow: hidden;
		}
	</style>

JS代码

<script type="text/javascript">
		//this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
		function Fireworks(x,y){//x,y鼠标的位置
			this.x=x;
			this.y=y;
			var that=this;
			//1.创建烟花。
			this.ceratefirework=function(){
				this.firework=document.createElement('div');//整个构造函数内部都可以使用
				this.firework.style.cssText=`width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
				document.body.appendChild(this.firework);
				this.fireworkmove();
			};
			//2.烟花运动和消失
			this.fireworkmove=function(){
				var that=this;
				buffermove(this.firework,{top:this.y},function(){
					document.body.removeChild(that.firework);//烟花消失,碎片产生
					that.fireworkfragment();
				});
			};
			//3.创建烟花的碎片
			this.fireworkfragment=function(){
				var num=this.ranNum(30,60);//盒子的个数
				this.perRadio=2*Math.PI/num;//弧度
				for(var i=0;i<num;i++){
					this.fragment=document.createElement('div');
					this.fragment.style.cssText=`width:5px;height:5px;background:rgb(${this.ranNum(0,255)},${this.ranNum(0,255)},${this.ranNum(0,255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
					document.body.appendChild(this.fragment);
					this.fireworkboom(this.fragment,i);//将当前创建的碎片传过去,方便运动和删除
				}
			}

			//4.碎片运动
			this.fireworkboom=function(obj,i){//obj:创建的碎片
				var r=10;
				obj.timer=setInterval(function(){//一个盒子运动
					r+=4;
					if(r>=200){
						clearInterval(obj.timer);
						document.body.removeChild(obj);
					}
					obj.style.left=that.x+Math.sin(that.perRadio*i)*r+'px';
					obj.style.top=that.y+Math.cos(that.perRadio*i)*r+'px';
				},20);
				
			}

			//随机方法
			this.ranNum=function (min,max){
				return Math.round(Math.random()*(max-min))+min;
			};
		}


		document.onclick=function(ev){
			var ev=ev||window.event;
			new Fireworks(ev.clientX,ev.clientY).ceratefirework();
		}
	</script>
	

三、爱心形烟花

效果展示
在这里插入图片描述

HTML代码

引入js 文件

<script type="text/javascript" src="buffermove1.js"></script>

CSS代码

创建一个黑色背景

<style type="text/css">
		*{
			padding: 0px;margin: 0px;
		}

		body{
			background: #000;
			width: 100%;
			height:100%;
			overflow: hidden;
		}
	</style>

JS代码

<script type="text/javascript">
		//this绑定的属性可以在整个构造函数内部都可以使用,而变量只能在函数内部使用。
		function Fireworks(x,y){//x,y鼠标的位置
			this.x=x;
			this.y=y;
			var that=this;
			//1.创建烟花。
			this.ceratefirework=function(){
				this.firework=document.createElement('div');//整个构造函数内部都可以使用
				this.firework.style.cssText=`width:5px;height:5px;background:#fff;position:absolute;left:${this.x}px;top:${document.documentElement.clientHeight}px;`;
				document.body.appendChild(this.firework);
				this.fireworkmove();
			};
			//2.烟花运动和消失
			this.fireworkmove=function(){
				buffermove(this.firework,{top:this.y},function(){
					document.body.removeChild(that.firework);//烟花消失,碎片产生
					that.fireworkfragment();
				});
			};
			//3.创建烟花的碎片
			this.fireworkfragment=function(){
				var num=this.ranNum(30,60);//盒子的个数
				this.perRadio=2*Math.PI/num;//弧度
				for(var i=0;i<num;i++){
					this.fragment=document.createElement('div');
					this.fragment.style.cssText=`width:5px;height:5px;background:rgb(${this.ranNum(0,255)},${this.ranNum(0,255)},${this.ranNum(0,255)});position:absolute;left:${this.x}px;top:${this.y}px;`;
					document.body.appendChild(this.fragment);
					this.fireworkboom(this.fragment,i);//将当前创建的碎片传过去,方便运动和删除
				}
			}


			
		    //x=16*Math.pow(sint,3);  //Math.sin(perRadio*i)
		    //y=13Cost-5*Cos2t-2*Cos3t-Cos4t
			//4.碎片运动
			this.fireworkboom=function(obj,i){//obj:创建的碎片
				var r=0.1;
				obj.timer=setInterval(function(){//一个盒子运动
					r+=0.4;
					if(r>=10){
						clearInterval(obj.timer);
						document.body.removeChild(obj);
					}
					obj.style.left=that.x+16*Math.pow(Math.sin(that.perRadio*i),3)*r+'px';
					obj.style.top=that.y-(13*Math.cos(that.perRadio*i)-5*Math.cos(2*that.perRadio*i)-2*Math.cos(3*that.perRadio*i)-Math.cos(4*that.perRadio*i))*r+'px';
				},20);
				
			}

			//随机方法
			this.ranNum=function (min,max){
				return Math.round(Math.random()*(max-min))+min;
			};
		}


		document.onclick=function(ev){
			var ev=ev||window.event;
			new Fireworks(ev.clientX,ev.clientY).ceratefirework();
		}
	</script>
	

四、源码获取

在线下载

资源链接:https://gitee.com/huang_weifu/JavaScript_demo.git
在这里插入图片描述

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的HTML+CSS+JS网页制作成品的示例: HTML文件(index.html): ```html <!DOCTYPE html> <html> <head> <title>My Website</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, diam eget bibendum bibendum, velit elit ultricies leo, vel bibendum sapien sapien nec sapien. Sed euismod, diam eget bibendum bibendum, velit elit ultricies leo, vel bibendum sapien sapien nec sapien.</p> </section> <section> <h2>Our Services</h2> <ul> <li>Service 1</li> <li>Service 2</li> <li>Service 3</li> </ul> </section> </main> <footer> <p>© 2021 My Website</p> </footer> <script src="script.js"></script> </body> </html> ``` CSS文件(style.css): ```css body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #333; color: #fff; padding: 20px; } nav ul { list-style: none; margin: 0; padding: 0; } nav li { display: inline-block; margin-right: 20px; } nav a { color: #fff; text-decoration: none; } main { padding: 20px; } section { margin-bottom: 20px; } h2 { color: #333; } footer { background-color: #333; color: #fff; padding: 20px; text-align: center; } ``` JS文件(script.js): ```javascript // 点击导航栏链接时滚动到相应的部分 document.querySelectorAll('nav a').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芒果Cake

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值