javascript特效——烟花燃放的效果[xyytit]
春节临近,要做活动促销专题页面,百度了下,找到一些烟花燃放的特效,整理并添加了修改烟花各种参数的注释,便于大家修改使用,版权归原作者所有。
1. 示例效果:
下载源码:点击这里
2. Html代码:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script type="text/javascript" src="firework.js"></script>
- <script type="text/javascript">
- function test() {
- var fireworks=[];
- var total=15;
- //控制烟花散开的频率
- window.setInterval(function () {
- for (var i = 0; i < total; i++) {
- if (!fireworks[i] || !fireworks[i].parentElement) {
- var x = Utils.getIntervalRandom(50, document.body.offsetWidth - 50);
- var shotHeight = Utils.getIntervalRandom(100, 450);//控制烟花散开的高度范围
- var radius = Utils.getIntervalRandom(50, 200);//控制烟花散开的半径
- var particleCount = Utils.getIntervalRandom(20, 100);//烟花散开的子点数
- var speed = Utils.getIntervalRandom(10, 200);//烟花散开的速度
- var color = "#" + Utils.getIntervalRandom(0, 16777215).toString(16).padLeft(6, "f");//控制烟花的颜色
- var size = Utils.getIntervalRandom(1, 28); //自定义添加烟花大小
- fireworks[i] = new Firework(x, shotHeight, radius, particleCount, color, speed, size);
- }
- }
- }, 100);
- //控制烟花燃放的频率
- window.setInterval(function(){
- var currentIndex=Utils.getIntervalRandom(0,total);
- if(fireworks[currentIndex] && fireworks[currentIndex].parentElement && !fireworks[currentIndex].isShoted){
- fireworks[currentIndex].shot();
- }
- },200);//控制烟花燃放速度,值越小,烟花越多
- }
- </script>
- </head>
- <body bgColor="#000000" onload="test();">
- <div style="width:100%;text-align:center;font:100px 'Comic Sans MS',Arial,sans-serif;color:yellow;">Happy New Year</div>
- <div style="width:100%;text-align:center;font:100px 'Comic Sans MS',Arial,sans-serif;color:red;">祝大家新年快乐!</div>
- </body>
- </html>
3. javascript代码:
- var Utils={};
- Utils.$E=function(id){
- return document.getElementById(id);
- };
- Utils.$C=function(tagName){
- return document.createElement(tagName);
- };
- Utils.getIntervalRandom=function(min,max){
- return parseInt(Math.random()*(max-min)+min);
- };
- Utils.INTERVAL_SPEED=30;//烟花上升速度,值越大,上升越慢
- if(navigator.appName.toLowerCase().indexOf("netscape")!=-1)
- {
- Utils.INTERVAL_SPEED=Utils.INTERVAL_SPEED+(Utils.INTERVAL_SPEED/6);
- }
- String.prototype.padLeft=function(sLen,padChar){
- var result=this;
- for(i=this.length;i<sLen;i++){
- result=padChar+result;
- }
- return result;
- };
- var Firework = function (x, shotHeight, radius, particleCount, color, speed, size) {
- this.shotHeight = shotHeight || 200;
- this.radius = radius || 100;
- this.particleCount = particleCount || 10;
- this.color = color || "#FFffff";
- this.parentElement = document.body;
- this.x = x;
- this.shottingSpeed = speed || 3;
- this.isShoted = false;
- this.isFlash = true;//是否有闪屏
- this.size = size;//自定义添加烟花大小尺寸
- this._particles = [];
- this._particleShape = unescape("•");//烟花显示的内容
- this._shottingShape = "|";
- this._depth = 3;
- this._shotting = Utils.$C("div");
- this._flashing = Utils.$C("div");
- this._disposeCount = 0;
- var _this = this;
- void function initialize() {
- for (var i = 0; i < _this.particleCount; i++) {
- _this._particles[i] = Utils.$C("div");
- _this._particles[i].style.position = "fixed";
- _this._particles[i].style.left = _this.x + "px";
- _this._particles[i].style.top = _this.shotHeight + "px";
- _this._particles[i].style.zIndex = 100;
- _this._particles[i].style.color = _this.color;
- _this._particles[i].style.display = "none";
- _this._particles[i].innerHTML = _this._particleShape;
- _this._particles[i].distance = Utils.getIntervalRandom(1, _this.radius - parseInt((i % _this._depth) * (_this.radius / _this._depth)));
- _this._particles[i].speed = Utils.getIntervalRandom(1, 4) * _this._particles[i].distance * 0.06;
- _this.parentElement.appendChild(_this._particles[i]);
- _this._setSize(_this._particles[i], _this.size);
- }
- _this._shotting.speed = _this.shottingSpeed;
- _this._shotting.innerHTML = _this._shottingShape;
- _this._shotting.style.position = "fixed";
- _this._shotting.style.fontWeight = "900";
- _this._shotting.style.left = _this.x + "px";
- //_this._shotting.style.top=_this.parentElement.offsetTop+_this.parentElement.offsetHeight-_this._shotting.offsetHeight+"px";
- _this._shotting.style.top = "700px";
- _this._shotting.style.zIndex = 100;
- _this._shotting.style.color = _this.color;
- _this._setSize(_this._shotting, 15);
- _this.parentElement.appendChild(_this._shotting);
- _this._flashing.style.width = "100%";
- _this._flashing.style.height = "100%";
- _this._flashing.style.left = "0";
- _this._flashing.style.top = "0";
- _this._flashing.style.backgroundColor = "#ffffee";
- _this._flashing.style.position = "fixed";
- _this._flashing.style.zIndex = 200;
- _this._flashing.style.display = "none";
- _this._flashing.style.MozOpacity = 0.5;
- _this._flashing.style.filter = "alpha(opacity=50)";
- _this.parentElement.appendChild(_this._flashing);
- } ();
- };
- Firework.prototype.shot=function(){
- var _this=this;
- _this.isShoted=true;
- var shotInterval=window.setInterval(function(){
- if(parseInt(_this._shotting.style.top)>_this.shotHeight){
- _this._shotting.style.top=parseInt(_this._shotting.style.top)-_this._shotting.speed+"px";
- }
- else{
- window.clearInterval(shotInterval);
- _this.parentElement.removeChild(_this._shotting);
- _this.bomb();
- _this._shotting=null;
- }
- },Utils.INTERVAL_SPEED);
- };
- Firework.prototype.bomb=function(){
- var _this=this;
- if(_this.isFlash){
- _this._flashing.style.display="";
- var flashTimeout=window.setTimeout(function(){
- _this.parentElement.removeChild(_this._flashing);
- window.clearTimeout(flashTimeout);
- },10);//闪屏闪烁频率,值越小,闪烁越快
- }
- else{
- _this.parentElement.removeChild(_this._flashing);
- }
- for (var i = 0; i <_this._particles.length; i++) {
- _this._moveParticle(_this._particles[i], Utils.getIntervalRandom(0,360));
- }
- };
- Firework.prototype._setSize=function(obj,value){
- obj.style.fontSize=parseInt(value)+"px";
- };
- Firework.prototype._moveParticle=function(particle,angle){
- var _this=this;
- var initX=parseInt(particle.style.left);
- var initY=parseInt(particle.style.top);
- var currentDistance=0;
- var currentX=initX;
- var currentY=initY;
- particle.style.display="";
- particle.intervalId=window.setInterval(function(){
- if(currentDistance<particle.distance){
- var newX,newY;
- var xAngle=angle*(2*Math.PI/360);
- var xDirection=Math.abs(Math.cos(xAngle))/Math.cos(xAngle);
- var yDirection=Math.abs(Math.sin(xAngle))/Math.sin(xAngle);
- if(Math.abs(Math.tan(xAngle))<=1){
- var deltaX=Math.abs(particle.speed*Math.cos(xAngle))*xDirection;
- newX=currentX+deltaX;
- newY=-(newX-initX)*Math.tan(xAngle)+initY;
- currentDistance+=Math.abs(deltaX);
- }
- else{
- var deltaY=Math.abs(particle.speed*Math.sin(xAngle))*yDirection;
- newY=currentY-deltaY;
- newX=-(newY-initY)/Math.tan(xAngle)+initX;
- currentDistance+=Math.abs(deltaY);
- }
- currentX=newX;
- currentY=newY;
- particle.style.left=currentX+"px";
- particle.style.top=currentY+"px";
- }
- else{
- window.clearInterval(particle.intervalId);
- _this.parentElement.removeChild(particle);
- particle=null;
- if(++_this._disposeCount==_this.particleCount){
- _this._particles.length=0;
- _this.parentElement=null;
- _this=null;
- }
- }
- },Utils.INTERVAL_SPEED);
- };
javascript特效——烟花燃放的效果[xyytit]的更多相关文章
- iOS 烟花撒花效果,图层渐变,图层倒影特效。CAEmitterLayer粒子发射器
iOS 烟花撒花效果,图层渐变,图层倒影特效.CAEmitterLayer粒子发射器 上一节我写了一个关于视图图层的相关类,有关CALayer这个类的使用和一些使用方法,详细看这里,就是我们在处理视图 ...
- 原生JavaScript 全特效微博发布面板效果实现
javaScript实现微博发布面板效果.---转载白超华 采用的js知识有: 正则表达式区分中英文字节.随机数生成等函数 淡入淡出.缓冲运动.闪动等动画函数 onfocus.onblur.oninp ...
- Canvas与javaScript特效笔记
第六章 Canvas与javaScript特效笔记 q <canvas>标签的用途 HTML5 canvas 提供了通过 JavaScript 绘制图形的方法,此方法使用简单但功能强 ...
- Javascript特效代码大全(420个)(转)
转载自:Javascript特效代码大全(420个) 收集资料,以便使用+面试+学习 ├ Cookie脚本 ├ 随访问次数变提示 ├ 集成Cookies ├ 使窗口仅弹出一次 ├ 签名提示程序 ├ ...
- 【JavaScript】在同一个网页中实现多个JavaScript特效
在网页中,假设出现两次<script type="text/javascript"></script>标签,全部的JavaScipt脚本都不会再生效,仅仅能 ...
- 10种JavaScript特效实例让你的网站更吸引人
我们有三种主要的方法(从难到易):自己动手写脚本;使用类似于jQuery和mooTools的JavaScript框架(可以让编写代码变得更容易些);使用能工作于现有的JavaScript框架下的提前预 ...
- JavaScript特效源码(1、文字特效)
注:本文以及以下关于Javascript特效源码都是分享自JavaScript源码大全. 1.逐隐逐现的的特效 逐隐逐现的文字特效[推荐使用][适用于IE4++] (修改显示的文字后根据说明进行共2步 ...
- HTML5夜空烟花绽放动画效果
模板描述:HTML5夜空烟花绽放动画效果基于HTML5 canvas制作,模拟夜空烟花绽放动画效果,烟花会在夜空打出贺词,有新年快乐.合家幸福.万事如意.心想事成.财源广进等,文字可以自定义,做成各种 ...
- JavaScript特效(调试笔记)
JavaScript特效 一.在网页上显示当前的时间日期,例如:“2016年3月26日 星期六”. js源代码: function getTime() { var today = new Date() ...
随机推荐
- 【转】扫盲 同步利器、分布式网盘--BT Sync
原文地址:http://program-think.blogspot.com/2015/01/BitTorrent-Sync.html先向大伙儿宣布个好消息——经过多位热心读者的大力支持,经过几天的努 ...
- .net 模拟登录Post提交
最近在做一个项目,要求集成到第三方系统中,由于先前没有做过类似的活,所以折腾了几天,蹭着有闲情的时候记录一下. 以下实例,都是我用Asp.net语言进行开发的,关于HTML元素的获取,使用的是Goog ...
- yii create url (一)
1.$this->redirect这里的$this是当前的controller.可能是应用程序的也 可能是模块下的 这里仅将解一下第一个参能是url,当url是一个字符串时,它会自己动跳转 如$ ...
- Android之登录时密码的保护
在很多的Android项目中都需要用户登录.注册.这样的话在开发中做好保护用户密码的工作就显得尤为重要.这里我把自己的密码保护方法记录下来. 这是我建了一个保存密码的文件,以便于检查自己保存密码或者上 ...
- Android - ADB 的使用
一.什么是ADB? 1.ADB全称Android Debug Bridge, 是android sdk里的一个工具,用这个工具可以直接操作管理android模拟器或者真实的andriod设备 2.AD ...
- c语言模拟实现oc引用计数
#include<stdio.h> #include<stdlib.h> //在c中引入 引用计数机制 // 要解决的问题: 1,指向某块动态内存的指针有几个? // ...
- 认识Activity,创建第一个android应用-Hello Word
2016-04-05 配置好Java.eclipse和Android环境就花费了一天时间.下载SDK真是费了不少时间.现在终于找到解决SDK更新的好方法了(更新自己电脑上的hosts文件,就可以使用G ...
- [转]三大WEB服务器对比分析(apache ,lighttpd,nginx)
原博文地址:http://www.blogjava.net/daniel-tu/archive/2008/12/29/248883.html 一.软件介绍(apache lighttpd ngin ...
- 15、安全工程师要阅读的书籍 - IT软件人员书籍系列文章
信息安全工程师是一个比较新兴的角色.在2016年今年的下半年软考就将安全工程师纳入了考试科目,说明国家对安全工程师的需求还是不错的.安全工程师包括硬件和软件两块内容吧.这里描述的安全工程师主要是针对软 ...
- header("location:test.php")跳转成功需要注意的
header("location:test.php")跳转成功除了需要注意以下三点还有一个前提必须要注意: 1.location和":"号间不能有空格,否则会出 ...