1、依赖源码

(function($){$.fn.fireworks=function(options){options=options||{};options.opacity=options.opacity||1;options.width=options.width||$(this).width();options.height=options.height||$(this).height();options.resistance=options.resistance||1;var fireworksField=this,particles=[],rockets=[],MAX_PARTICLES=10000,SCREEN_WIDTH=options.width,SCREEN_HEIGHT=options.height;var canvas=document.createElement('canvas');canvas.id=options.fireworksId;canvas.width=SCREEN_WIDTH;canvas.height=SCREEN_HEIGHT;canvas.style.width=SCREEN_WIDTH+'px';canvas.style.height=SCREEN_HEIGHT+'px';canvas.style.position='absolute';canvas.style.top='0px';canvas.style.left='0px';canvas.style.zIndex='99';canvas.style.opacity=options.opacity;var context=canvas.getContext('2d');function Particle(pos){this.pos={x:pos?pos.x:0,y:pos?pos.y:0};this.vel={x:0,y:0};this.shrink=0.97;this.size=2;this.resistance=options.resistance;this.gravity=0;this.flick=false;this.alpha=1;this.fade=0;this.color=0}Particle.prototype.update=function(){this.vel.x*=this.resistance;this.vel.y*=this.resistance;this.vel.y+=this.gravity;this.pos.x+=this.vel.x;this.pos.y+=this.vel.y;this.size*=this.shrink;this.alpha-=this.fade};Particle.prototype.render=function(c){if(!this.exists()){return}c.save();c.globalCompositeOperation='lighter';var x=this.pos.x,y=this.pos.y,r=this.size/2;var gradient=c.createRadialGradient(x,y,0.1,x,y,r);gradient.addColorStop(0.1,"rgba(255,255,255,"+this.alpha+")");gradient.addColorStop(0.8,"hsla("+this.color+", 100%, 50%, "+this.alpha+")");gradient.addColorStop(1,"hsla("+this.color+", 100%, 50%, 0.1)");c.fillStyle=gradient;c.beginPath();c.arc(this.pos.x,this.pos.y,this.flick?Math.random()*this.size:this.size,0,Math.PI*2,true);c.closePath();c.fill();c.restore()};Particle.prototype.exists=function(){return this.alpha>=0.1&&this.size>=1};function Rocket(x){Particle.apply(this,[{x:x,y:SCREEN_HEIGHT}]);this.explosionColor=0}Rocket.prototype=new Particle();Rocket.prototype.constructor=Rocket;Rocket.prototype.explode=function(){var count=Math.random()*10+80;for(var i=0;i<count;i++){var particle=new Particle(this.pos);var angle=Math.random()*Math.PI*2;var speed=Math.cos(Math.random()*Math.PI/2)*15;particle.vel.x=Math.cos(angle)*speed;particle.vel.y=Math.sin(angle)*speed;particle.size=10;particle.gravity=0.2;particle.resistance=0.92;particle.shrink=Math.random()*0.05+0.93;particle.flick=true;particle.color=this.explosionColor;particles.push(particle)}};Rocket.prototype.render=function(c){if(!this.exists()){return}c.save();c.globalCompositeOperation='lighter';var x=this.pos.x,y=this.pos.y,r=this.size/2;var gradient=c.createRadialGradient(x,y,0.1,x,y,r);gradient.addColorStop(0.1,"rgba(255, 255, 255 ,"+this.alpha+")");gradient.addColorStop(1,"rgba(0, 0, 0, "+this.alpha+")");c.fillStyle=gradient;c.beginPath();c.arc(this.pos.x,this.pos.y,this.flick?Math.random()*this.size/2+this.size/2:this.size,0,Math.PI*2,true);c.closePath();c.fill();c.restore()};var loop=function(){if(SCREEN_WIDTH!=window.innerWidth){canvas.width=SCREEN_WIDTH=window.innerWidth}if(SCREEN_HEIGHT!=window.innerHeight){canvas.height=SCREEN_HEIGHT=window.innerHeight}context.fillStyle="rgba(0, 0, 0, 0.05)";context.fillRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);var existingRockets=[];for(var i=0;i<rockets.length;i++){rockets[i].update();rockets[i].render(context);var distance=Math.sqrt(Math.pow(SCREEN_WIDTH-rockets[i].pos.x,2)+Math.pow(SCREEN_HEIGHT-rockets[i].pos.y,2));var randomChance=rockets[i].pos.y<(SCREEN_HEIGHT*2/3)?(Math.random()*100<=1):false;if(rockets[i].pos.y<SCREEN_HEIGHT/5||rockets[i].vel.y>=0||distance<50||randomChance){rockets[i].explode()}else{existingRockets.push(rockets[i])}}rockets=existingRockets;var existingParticles=[];for(i=0;i<particles.length;i++){particles[i].update();if(particles[i].exists()){particles[i].render(context);existingParticles.push(particles[i])}}particles=existingParticles;while(particles.length>MAX_PARTICLES){particles.shift()}};var launchFrom=function(x){if(rockets.length<10){var rocket=new Rocket(x);rocket.explosionColor=Math.floor(Math.random()*360/10)*10;rocket.vel.y=Math.random()*-3-4;rocket.vel.x=Math.random()*6-3;rocket.size=8;rocket.shrink=0.999;rocket.gravity=0.01;rockets.push(rocket)}};var launch=function(){launchFrom(SCREEN_WIDTH/2)};$(fireworksField).append(canvas);var t=100;for(var i=0;i<options.nums;i++,t+=200){setTimeout(launch,t)}var loopInterval=setInterval(loop,10);var fireworksAnimationIl=true;setTimeout(function(){fireworksAnimationIl=setInterval(function(){if(particles.length==0){clearInterval(loopInterval);clearInterval(fireworksAnimationIl);options.fn()}},800)},t);return fireworksField}}(jQuery));

2、使用

$('#id, .class, tag').fireworks({
fireworksId: 'fireworksId', // 图层ID,不能重复
opacity: 0.9, // 背景透明度
resistance: 1,
nums: 24, // 烟花数量
width: '100%',
height: '100%',
fn: function () {
// 烟花方映完毕回调
}
});

3、效果

jQuery烟花效果的更多相关文章

  1. css3线条围绕跑马+jquery打字机效果

    原文地址:css3线条围绕跑马+jquery打字机效果 有图有真相,今天偶然看到了一种效果,仔细看了下,发现它是用css的clip+css3的动画实现的,简直叼.于是自己拿来了前一阵子写的打字机效果, ...

  2. JQuery动画效果

    jquery动画效果常用方法 1.show()显示效果语法:show(speed,callback)Number/String,Function speend为动画执行时间,单位为毫秒.也可以为slo ...

  3. 分享5种风格的 jQuery 分页效果【附代码】

    jPaginate 是一款非常精致的分页插件,提供了五种不同风格的分页效果,支持鼠标悬停翻页,快速分页功能.这款插件还提供了丰富的配置选项,你可以根据需要进行设置. 效果演示      源码下载 各个 ...

  4. 新鲜出炉的30个精美的 jQuery & CSS3 效果【附演示和教程】

    新鲜出炉的30个精美的 jQuery & CSS3 效果[附演示和教程]   作为最流行的 JavaScript 开发框架,jQuery 在现在的 Web 开发项目中扮演着重要角色,它简化了 ...

  5. jquery动画效果---animate()--滚屏

    jquery动画效果---animate()方法---W3school

  6. jquery 之效果

    // jquery 之效果 .css()既可以获取值,如 .css('fontSize'), 又可以设置内置属性,既可用驼峰式,也可以用连字符版,如 .css('background-color', ...

  7. 『HTML5梦幻之旅』-缤纷多姿的烟花效果

    天花无数月中开,五采祥云绕绛台.堕地忽惊星彩散,飞空旋作雨声来.怒撞玉斗翻晴雪,勇踏金轮起疾雷.更漏已深人渐散,闹竿挑得彩灯回. ——明·瞿佑·<烟火戏> 记得每年过春节的那段时间,除了欣 ...

  8. WEB烟花效果——Canvas实现

    摘要        本文主要介绍一种WEB形式的烟花(fireworks)效果(图1所示),该效果基于Canvas实现,巧妙地运用了canvas绘图的特性,并加入了物理力作用的模拟,使整体效果非常绚丽 ...

  9. python 全栈开发,Day53(jQuery的介绍,jQuery的选择器,jQuery动画效果)

    js总结 js: 1.ECMAScript5 ES5语法 2.DOM CRUD 获取 3种方式 id tag className //面向对象 对象 : 属性和方法 某个对象中 function $( ...

  10. jQuery的效果(动画)

    jquery的效果(动画) show 概念:显示隐藏的匹配元素 语法:show(speed,callback) 参数:speed:三种预定速度之一的字符串('slow','normal','fast' ...

随机推荐

  1. Git、TortoiseGit中文安装教程,如何注册Gitee账号进行代码提交,上传代码后主页贡献度没显示绿点(详解)

    今天给大家分享的是 Git 软件和 TortoiseGit 图形化软件的详细安装教程以及如何在 gitee 上进行代码提交. 首先我也是个刚接触 gitee 的一个小白用户,这些都是自己一边学一边记录 ...

  2. 你的哪些骚操作会导致Segmentation Fault😂

    你的哪些骚操作会导致Segmentation Fault 前言 如果你是一个写过一些C程序的同学,那么很大可能你会遇到魔幻的segmentation fault,可能一时间抓耳挠腮,本篇文章主要介绍一 ...

  3. PHP redis有序集合实现分页

    <?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 6379); //设置 redis 字符 ...

  4. 一天十道Java面试题----第二天(HashMap和hashTable的区别--------》sleep、wait、join)

    这里是参考B站上的大佬做的面试题笔记.大家也可以去看视频讲解!!! 文章目录 11.HashMap和HashTable的区别及底层实现 12.ConcurrentHashMap原理简述,jdk7和jd ...

  5. 探究Presto SQL引擎(4)-统计计数

    作者:vivo互联网用户运营开发团队 -  Shuai Guangying 本篇文章介绍了统计计数的基本原理以及Presto的实现思路,精确统计和近似统计的细节及各种优缺点,并给出了统计计数在具体业务 ...

  6. Python处理刚刚,分钟,小时,天前等时间

    简介 用爬虫获取目标网站数据后可能会遇见时间为处理刚刚,分钟,小时,天前等时间格式,如图 解决问题: 写了一个工具类来处理该问题,其中封装了两个函数 1. 将时间中的中文数字转换成阿拉伯数字 def ...

  7. python的一些运算符

    # 1.算术运算符 print('1.算术运算符') # 1.1 + 求和 a = 10 b = 20 c = a + b print(c) print('a+b={}'.format(c)) pri ...

  8. csharp 基础

    1.C#基础 1.1 C#简介 C#是一个面向对象的.由微软开发的高级编程语言,它专门为公共语言基础结构(CLI)设计的,CLI是由可执行代码和运行时环境组成的. C#语言在编写时有点像JAVA,在运 ...

  9. MongoDB导入导出备份数据

    需要提前安装mongodb-database-tools参考:centos离线安装mongodb-database-tools 导出数据 常用的导出有两种:mongodump和mongoexport, ...

  10. 关于C#Section配置未初始化的问题

    转载 https://www.cnblogs.com/lxshwyan/p/10828305.html 如果使用了configSection节点,则configSection必须位于根节点的第0个.A ...