<! 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> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> js fireworks effect (object-oriented edition) </title> <Style type = "text/css"> Html, body {overflow: hidden ;} Body, div, p {margin: 0; padding: 0 ;} Body {background: #000; font: 12px/1.5 arial; color: # 7A7A7A ;} A {text-decoration: none; outline: none ;} # Tips, # copyright {position: absolute; width: 100%; height: 50px; text-align: center; background: #171717; border: 2px solid #484848 ;} # Tips {top: 0; border-width: 0 0 2px ;} # Tips a {font: 14px/30px arial; color: # FFF; background: # F06; display: inline-block; margin: 10px 5px 0; padding: 0 15px; border-radius: 15px ;} # Tips a. active {background: # FE0000 ;} # Copyright {bottom: 0; line-height: 50px; border-width: 2px 0 0 ;} # Copyright a {color: # FFF; background: # 7A7A7A; padding: 2px 5px; border-radius: 10px ;} # Copyright a: hover {background: # F90 ;} P {position: absolute; top: 55px; width: 100%; text-align: center ;} </Style> <Script type = "text/javascript"> Var fgm = { On: function (element, type, handler ){ Return element. addEventListener? Element. addEventListener (type, handler, false): element. attachEvent ("on" + type, handler) }, Un: function (element, type, handler ){ Return element. removeEventListener? Element. removeEventListener (type, handler, false): element. detachEvent ("on" + type, handler) }, Bind: function (object, handler ){ Return function (){ Return handler. apply (object, arguments) } }, RandomRange: function (lower, upper ){ Return Math. floor (Math. random () * (upper-lower + 1) + lower) }, GetRanColor: function (){ Var str = this. randomRange (0, 0 xFFFFFF). toString (16 ); While (str. length <6) str = "0" + str; Return "#" + str } }; // Initialize the object Function FireWorks (){ This. type = 0; This. timer = null; This. fnManual = fgm. bind (this, this. manual) } FireWorks. prototype = { Initialize: function (){ ClearTimeout (this. timer ); Fgm. un (document, "click", this. fnManual ); Switch (this. type ){ Case 1: Fgm. on (document, "click", this. fnManual ); Break; Case 2: This. auto (); Break; }; }, Manual: function (event ){ Event = event | window. event; This. _ create __({ X: event. clientX, Y: event. clientY }); }, Auto: function () { Var that = this; That. timer = setTimeout (function (){ That. _ create __({ X: fgm. randomRange (50, document.doc umentElement. clientWidth-50 ), Y: fgm. randomRange (50, document.doc umentElement. clientHeight-150) }) That. auto (); }, Fgm. randomRange (900,110 0 )) }, _ Create __: function (param) { Var that = this; Var oEntity = null; Var oChip = null; Var aChip = []; Var timer = null; Var oFrag = document. createDocumentFragment (); OEntity = document. createElement ("div "); With (oEntity. style ){ Position = "absolute "; Top = document.doc umentElement. clientHeight + "px "; Left = param. x + "px "; Width = "4px "; Height = "30px "; BorderRadius = "4px "; Background = fgm. getRanColor (); }; Document. body. appendChild (oEntity ); OEntity. timer = setInterval (function (){ OEntity. style. top = oEntity. offsetTop-20 + "px "; If (oEntity. offsetTop <= param. y ){ ClearInterval (oEntity. timer ); Document. body. removeChild (oEntity ); (Function (){ // Randomly generates fragments between 50- // Due to the low processing efficiency of IE browser, the random range is reduced to 20-30 // When the fireworks are automatically set, the random range is reduced to 20-30. Var len = (/msie/I. test (navigator. userAgent) | that. type = 2 )? Fgm. randomRange (20, 30): fgm. randomRange (50,100) For (I = 0; I <len; I ++ ){ OChip = document. createElement ("div "); With (oChip. style ){ Position = "absolute "; Top = param. y + "px "; Left = param. x + "px "; Width = "4px "; Height = "4px "; Overflow = "hidden "; BorderRadius = "4px "; Background = fgm. getRanColor (); }; OChip. speedX = fgm. randomRange (-20, 20 ); OChip. speedY = fgm. randomRange (-20, 20 ); OFrag. appendChild (oChip ); AChip [I] = oChip }; Document. body. appendChild (oFrag ); Timer = setInterval (function (){ For (I = 0; I <aChip. length; I ++ ){ Var obj = aChip [I]; With (obj. style ){ Top = obj. offsetTop + obj. speedY + "px "; Left = obj. offsetLeft + obj. speedX + "px "; }; Obj. speedY ++; (Obj. offsetTop <0 | obj. offsetLeft <0 | obj. offsetTop> document.doc umentElement. clientHeight | obj. offsetLeft> document.doc umentElement. clientWidth) & (document. body. removeChild (obj), aChip. splice (I, 1 )) }; ! AChip [0] & clearInterval (timer ); }, 30) })() } }, 30) } }; Fgm. on (window, "load", function (){ Var oTips = document. getElementById ("tips "); Var aBtn = oTips. getElementsByTagName (""); Var oFireWorks = new FireWorks (); Fgm. on (oTips, "click", function (event ){ Var oEvent = event | window. event; Var oTarget = oEvent.tar get | oEvent. srcElement; Var I = 0; If (oTarget. tagName. toUpperCase () = ""){ For (I = 0; I <aBtn. length; I ++) aBtn [I]. className = ""; Switch (oTarget. id ){ Case "manual ": OFireWorks. type = 1; Break; Case "auto ": OFireWorks. type = 2; Break; Case "stop ": OFireWorks. type = 0; Break; } OFireWorks. initialize (); OTarget. className = "active "; OEvent. stopPropagation? OEvent. stopPropagation (): oEvent. cancelBubble = true } }); }); Fgm. on (document, "contextmenu", function (event ){ Var oEvent = event | window. event; OEvent. preventDefault? OEvent. preventDefault (): oEvent. returnValue = false }); </Script> </Head> <Body> <Div id = "tips"> <a id = "manual" href = "javascript :; "> manually set fireworks </a> <a id =" auto "href =" javascript :; "> automatic fireworks </a> <a id =" stop "href =" javascript:; "> stop fireworks </a> </div> <P> due to the limited rendering capability of the browser, the range of randomly generated fireworks fragments is automatically adjusted to 20-30 when the browser is set to IE or automatically set to fireworks. </p> <Div id = "copyright"> we recommend using Firefox and Chrome to preview the effect. By-Ferris, QQ: 21314130 </div> </Body> </Html> |