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

 更新时间:2015年03月02日 10:47:16   作者:站长学院  
这篇文章主要介绍了JS实现超炫网页烟花动画效果的方法,实例分析了javascript实现烟花动画效果的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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验证电子邮箱的函数

    JavaScript验证电子邮箱的函数

    本文主要是javascript使用正则来验证电子邮箱的一个函数分享,很简单,但也很使用,在很多地方都可以用的到。
    2014-08-08
  • JS.GetAllChild(element,deep,condition)使用介绍

    JS.GetAllChild(element,deep,condition)使用介绍

    JS.GetAllChild()获取所有子节点,想必大家都知道吧,具体的使用方法如下,感兴趣的朋友可以参考下
    2013-09-09
  • Webpack 服务器端代码打包的示例代码

    Webpack 服务器端代码打包的示例代码

    本篇文章主要介绍了Webpack 服务器端代码打包的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 使用微信SDK自定义分享的方法

    使用微信SDK自定义分享的方法

    这篇文章主要介绍了使用微信SDK自定义分享的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 通过函数作用域和块级作用域看javascript的作用域链

    通过函数作用域和块级作用域看javascript的作用域链

    这篇文章给大家分享了通过函数作用域和块级作用域看javascript的作用域链的相关知识点内容,有兴趣的朋友参考学习下。
    2018-08-08
  • 浅谈JS运算符&&和|| 及其优先级

    浅谈JS运算符&&和|| 及其优先级

    下面小编就为大家带来一篇浅谈JS运算符&&和|| 及其优先级。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • 小白谈谈对JS原型链的理解

    小白谈谈对JS原型链的理解

    这篇文章主要介绍了小白谈谈对JS原型链的理解的相关资料,需要的朋友可以参考下
    2016-05-05
  • 微信小程序wx:for 的使用及wx:key绑定两种方式

    微信小程序wx:for 的使用及wx:key绑定两种方式

    wx:for是微信小程序中的一个列表渲染指令,用于循环渲染一个数组或对象中的数据,它类似于JavaScript中的for循环,可以根据数据的长度自动渲染相应的列表项,本文给大家介绍微信小程序wx:for 的使用及wx:key绑定两种方式,感兴趣的朋友一起看看吧
    2023-12-12
  • 微信企业号开发之微信考勤Cookies的使用

    微信企业号开发之微信考勤Cookies的使用

    使用微信考勤,每次使用微信企业号开发:微信用户信息和web网页的session的关系这个里边的方法,调用微信的接口,有点慢,微信官方也推荐使用Cookies,但如何使用Cookies,自己却一直没有搞清楚。下面小编帮大家解决难题,需要的朋友可以参考下
    2015-09-09
  • layui表格 返回的数据状态异常的解决方法

    layui表格 返回的数据状态异常的解决方法

    今天小编就为大家分享一篇layui表格 返回的数据状态异常的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09

最新评论