本文實例講述了JS實現超炫網頁煙火動畫效果的方法。分享給大家供大家參考。具體分析如下: 非常炫的使用JS實現的一個網頁煙火燃放動畫效果,能適應JS做出這樣的動畫來 複製代碼 代碼如下: 超炫網頁煙火效果 <br /> .fire{display:block; overflow:hidden; font-size:12px; position:absolute};<br /> body{overflow:hidden; background:#000}<br /> html{overflow:hidden; background:#000}<br /> <br /> var Fire = function(r, color) {<br /> this.radius = r || 12;<br /> this.color = color;<br /> this.xpos = 0;<br /> this.ypos = 0;<br /> this.zpos = 0;<br /> this.vx = 0;<br /> this.vy = 0;<br /> this.vz = 0;<br /> this.mass = 1;<br /> this.x =0;<br /> this.y=0;<br /> this.p = document.createElement("span");<br /> this.p.className = "fire";<br /> this.p.innerHTML = "*";<br /> this.p.style.fontSize = this.radius "px";<br /> this.p.style.color = "#" this.color;<br /> }<br /> Fire.prototype = {<br /> append: function(parent) {<br /> parent.appendChild(this.p);<br /> },<br /> setSize: function(scale) {<br /> this.p.style.fontSize = this.radius * scale "px";<br /> },<br /> setPosition:function(x, y) {<br /> this.p.style.left = x "px";<br /> this.p.style.top = y "px";<br /> },<br /> setVisible: function(b) {<br /> this.p.style.display = b ? "block" : "none";<br /> }<br /> }<br /> var fireworks = function() {<br /> var fires = new Array();<br /> var count = 150;<br /> var fl = 250;<br /> var vpx = 500;<br /> var vpy = 300;<br /> var gravity = .5;<br /> var floor = 200;<br /> var bounce = -.8;<br /> var timer;<br /> var wind = ((Math.floor(Math.random()*3) 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;<br /> var wpos = 0;<br /> var wcount;<br /> return {<br /> init: function() {<br /> wcount = 50 Math.floor(Math.random() * 100);<br /> for (var i=0; i<count; i ) {<br /> var color = 0xFF0000;<br /> color = (Math.random() * 0xFFFFFF).toString(16).toString().split(".")[0];<br /> while(color.length < 6) {<br /> color = "0" color;<br /> }<br /> var fire = new Fire(12, color);<br /> fires.push(fire);<br /> fire.ypos = -100;<br /> fire.vz = Math.random() * 6 - 3;<br /> fire.vx = (Math.random()*2 - 1)*2 ;<br /> fire.vy = Math.random()*-15 - 15;<br /> fire.x = 500<br /> fire.y = 600;<br /> fire.append(document.body);<br /> }<br /> var that = this;<br /> timer = setInterval(function() {<br /> wpos ;<br /> if (wpos >= wcount) {<br /> wpos = 0;<br /> wcount = 50 Math.floor(Math.random() * 100);<br /> wind = ((Math.floor(Math.random()*3) 3)/10)*(Math.random()*2 - 1 > 0 ? 1 : -1)*.25;<br /> }<br /> for (var i=0;i<count; i ) {<br /> that.move(fires[i]);<br /> }<br /> }, 30);<br /> },<br /> move: function(fire) {<br /> fire.vy = gravity;<br /> fire.x = fire.vx;<br /> fire.y = fire.vy;<br /> fire.vx = wind;<br /> fire.setPosition(fire.x, fire.y);<br /> if (fire.x < 0 || fire.x >1000 || fire.y < 0 || fire.y > 600) {<br /> fire.vx = (Math.random()*2 - 1)*2;<br /> fire.vy = Math.random()*-15 - 15;<br /> fire.x = 500;<br /> fire.y = 600;<br /> fire.setPosition(fire.x, fire.y);<br /> }<br /> }<br /> }<br /> }<br /> fireworks().init();<br /> 希望本文所述對大家的javascript程式設計有所幫助。