JS实现超炫网页烟花动画效果的方法


Posted in Javascript onMarch 02, 2015

本文实例讲述了JS实现超炫网页烟花动画效果的方法。分享给大家供大家参考。具体分析如下:

非常炫的使用JS实现的一个网页烟花燃放动画效果,能适应JS做出这样的动画来

<!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>

<title>超炫网页烟花效果</title>

</head>

<style type="text/css">

.fire{display:block; overflow:hidden; font-size:12px; position:absolute};

body{overflow:hidden; background:#000}

html{overflow:hidden; background:#000}

</style>

<body>

</body>

<script type="text/javascript">

var Fire = function(r, color) {

this.radius = r || 12;

this.color = color;

this.xpos = 0;

this.ypos = 0;

this.zpos = 0;

this.vx = 0;

this.vy = 0;

this.vz = 0;

this.mass = 1;

this.x =0;

this.y=0;

this.p = document.createElement("span");

this.p.className = "fire";

this.p.innerHTML = "*";

this.p.style.fontSize = this.radius + "px";

this.p.style.color = "#" + this.color;

}

Fire.prototype = {

append: function(parent) {

parent.appendChild(this.p);

},

setSize: function(scale) {

this.p.style.fontSize = this.radius * scale + "px";

},

setPosition:function(x, y) {

this.p.style.left = x + "px";

this.p.style.top =  y + "px";

},

setVisible: function(b) {

this.p.style.display = b ? "block" : "none";

}

}

var fireworks = function() {

var fires = new Array();

var count = 150;

var fl = 250;

var vpx = 500;

var vpy = 300;

var gravity = .5;

var floor = 200;

var bounce = -.8;

var timer;

var wind = ((Math.floor(Math.random()*3) + 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;

var wpos = 0;

var wcount;

return {

init: function() {

wcount = 50 + Math.floor(Math.random() * 100);

for (var i=0; i<count; i++) {

var color = 0xFF0000;

color = (Math.random() * 0xFFFFFF).toString(16).toString().split(".")[0];

while(color.length < 6) {

color = "0" + color;

}

var fire = new Fire(12, color);

fires.push(fire);

fire.ypos = -100;

fire.vz = Math.random() * 6 - 3;

fire.vx = (Math.random()*2 - 1)*2 ;

fire.vy = Math.random()*-15 - 15;

fire.x = 500

fire.y = 600;

fire.append(document.body);

}

var that = this;

timer = setInterval(function() {

wpos++;

if (wpos >= wcount) {

wpos = 0;

wcount = 50 + Math.floor(Math.random() * 100);

wind = ((Math.floor(Math.random()*3) + 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;

}

for (var i=0;i<count; i++) {

that.move(fires[i]);

}

}, 30);

},

move: function(fire) {

fire.vy += gravity;

fire.x += fire.vx;

fire.y += fire.vy;

fire.vx += wind;

fire.setPosition(fire.x, fire.y);

if (fire.x < 0 || fire.x >1000 || fire.y  < 0 || fire.y  > 600) {

fire.vx = (Math.random()*2 - 1)*2;

fire.vy = Math.random()*-15 - 15;

fire.x = 500;

fire.y = 600;

fire.setPosition(fire.x, fire.y);

}

}

}

}

fireworks().init();

</script>

</html>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
json 定义
Jun 10 Javascript
关于图片的预加载过程中隐藏未知的
Dec 19 Javascript
JavaScript验证18位身份证号码最后一位正确性的实现代码
Aug 07 Javascript
js生成验证码并直接在前端判断
May 15 Javascript
JQuery遍历DOM节点的方法
Jun 11 Javascript
Easyui ueditor 整合解决不能编辑的问题(推荐)
Jun 25 Javascript
详解vue 实例方法和数据
Oct 23 Javascript
vue 组件高级用法实例详解
Apr 11 Javascript
对Vue beforeRouteEnter 的next执行时机详解
Aug 25 Javascript
谈谈IntersectionObserver懒加载的具体使用
Oct 15 Javascript
vue prop传值类型检验方式
Jul 30 Javascript
vscode中使用npm安装babel的方法
Aug 02 Javascript
ECMAScript 5严格模式(Strict Mode)介绍
Mar 02 #Javascript
Javascript中使用A标签获取当前目录的绝对路径方法
Mar 02 #Javascript
JavaScript日期类型的一些用法介绍
Mar 02 #Javascript
了解Javascript的模块化开发
Mar 02 #Javascript
JS实现的4种数字千位符格式化方法分享
Mar 02 #Javascript
js实现图片漂浮效果的方法
Mar 02 #Javascript
ECMAScript 5中的属性描述符详解
Mar 02 #Javascript
You might like
PHP自动生成月历代码
2006/10/09 PHP
PHP在字符串中查找指定字符串并删除的代码
2008/10/02 PHP
Laravel框架实现文件上传的方法分析
2019/09/29 PHP
javascript:以前写的xmlhttp池,代码
2008/05/18 Javascript
关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
2010/10/18 Javascript
JavaScript中的类继承
2010/11/25 Javascript
基于jquery的$.ajax async使用
2011/10/19 Javascript
Javascript模块化编程(一)模块的写法最佳实践
2013/01/17 Javascript
JavaScript sub方法入门实例(把字符串显示为下标)
2014/10/17 Javascript
jQuery中parentsUntil()方法用法实例
2015/01/07 Javascript
JS中完美兼容各大浏览器的scrolltop方法
2015/04/17 Javascript
Eclipse引入jquery报错如何解决
2015/12/01 Javascript
全面解析Javascript无限添加QQ好友原理
2016/06/15 Javascript
JQuery PHP图片在线裁剪实例
2020/07/27 Javascript
node.js平台下利用cookie实现记住密码登陆(Express+Ejs+Mysql)
2017/04/26 Javascript
React Native 通告消息竖向轮播组件的封装
2020/08/25 Javascript
echarts整合多个类似option的方法实例
2018/07/10 Javascript
[01:03:27]Optic vs VGJ.S 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python中表达式x += y和x = x+y 的区别详解
2017/06/20 Python
老生常谈Python之装饰器、迭代器和生成器
2017/07/26 Python
Python中将dataframe转换为字典的实例
2018/04/13 Python
python单向循环链表原理与实现方法示例
2019/12/03 Python
Python 实现递归法解决迷宫问题的示例代码
2020/01/12 Python
python3 Scrapy爬虫框架ip代理配置的方法
2020/01/17 Python
python库skimage给灰度图像染色的方法示例
2020/04/27 Python
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
HTML5中5个简单实用的API
2014/04/28 HTML / CSS
介绍下Lucene建立索引的过程
2016/03/02 面试题
Java的类与C++的类有什么不同
2014/01/18 面试题
客户代表自我评价范例
2013/09/24 职场文书
学校食堂采购员岗位职责
2013/12/05 职场文书
微信营销策划方案
2014/02/24 职场文书
财务统计员岗位职责
2015/04/14 职场文书
个人工作失误的保证书怎么写?
2019/06/21 职场文书
SSM VUE Axios详解
2021/10/05 Vue.js
CentOS下安装Jenkins的完整步骤
2022/04/07 Servers