首页 > 语言 > JavaScript > 正文

JS基于面向对象实现的放烟花效果

2024-05-06 16:19:35
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JS基于面向对象实现的放烟花效果,涉及javascript面向对象技术的使用技巧,需要的朋友可以参考下

本文实例讲述了JS基于面向对象实现的放烟花效果。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  6. <title>js放烟花效果(面向对象版)</title> 
  7. <style type="text/css"> 
  8. html,body{overflow:hidden;} 
  9. body,div,p{margin:0;padding:0;} 
  10. body{background:#000;font:12px/1.5 arial;color:#7A7A7A;} 
  11. a{text-decoration:none;outline:none;} 
  12. #tips,#copyright{position:absolute;width:100%;height:50px;text-align:center;background:#171717;border:2px solid #484848;} 
  13. #tips{top:0;border-width:0 0 2px;} 
  14. #tips a{font:14px/30px arial;color:#FFF;background:#F06;display:inline-block;margin:10px 5px 0;padding:0 15px;border-radius:15px;} 
  15. #tips a.active{background:#FE0000;} 
  16. #copyright{bottom:0;line-height:50px;border-width:2px 0 0;} 
  17. #copyright a{color:#FFF;background:#7A7A7A;padding:2px 5px;border-radius:10px;} 
  18. #copyright a:hover{background:#F90;} 
  19. p{position:absolute;top:55px;width:100%;text-align:center;} 
  20. </style> 
  21. <script type="text/javascript"> 
  22. var fgm = { 
  23. on: function(element, type, handler) { 
  24. return element.addEventListener ? element.addEventListener(type, handler, false) : element.attachEvent("on" + type, handler) 
  25. }, 
  26. un: function(element, type, handler) { 
  27. return element.removeEventListener ? element.removeEventListener(type, handler, false) : element.detachEvent("on" + type, handler) 
  28. }, 
  29. bind: function(object, handler) { 
  30. return function() { 
  31. return handler.apply(object, arguments)  
  32. }  
  33. }, 
  34. randomRange: function(lower, upper) { 
  35. return Math.floor(Math.random() * (upper - lower + 1) + lower)  
  36. }, 
  37. getRanColor: function() { 
  38. var str = this.randomRange(0, 0xFFFFFF).toString(16); 
  39. while(str.length < 6) str = "0" + str; 
  40. return "#" + str  
  41. } 
  42. }; 
  43. //初始化对象 
  44. function FireWorks() { 
  45. this.type = 0; 
  46. this.timer = null; 
  47. this.fnManual = fgm.bind(this, this.manual) 
  48. } 
  49. FireWorks.prototype = { 
  50. initialize: function() { 
  51. clearTimeout(this.timer); 
  52. fgm.un(document, "click", this.fnManual); 
  53. switch(this.type) { 
  54. case 1: 
  55. fgm.on(document, "click", this.fnManual); 
  56. break; 
  57. case 2: 
  58. this.auto(); 
  59. break; 
  60. }; 
  61. }, 
  62. manual: function(event) { 
  63. event = event || window.event; 
  64. this.__create__({ 
  65. x: event.clientX, 
  66. y: event.clientY  
  67. }); 
  68. }, 
  69. auto: function () 
  70. { 
  71. var that = this; 
  72. that.timer = setTimeout(function() {  
  73. that.__create__({ 
  74. x: fgm.randomRange(50, document.documentElement.clientWidth - 50), 
  75. y: fgm.randomRange(50, document.documentElement.clientHeight - 150) 
  76. })  
  77. that.auto(); 
  78. }, fgm.randomRange(900, 1100)) 
  79. }, 
  80. __create__: function (param) 
  81. { 
  82. var that = this; 
  83. var oEntity = null; 
  84. var oChip = null; 
  85. var aChip = []; 
  86. var timer = null; 
  87. var oFrag = document.createDocumentFragment(); 
  88. oEntity = document.createElement("div"); 
  89. with(oEntity.style) { 
  90. position = "absolute"; 
  91. top = document.documentElement.clientHeight + "px"; 
  92. left = param.x + "px"; 
  93. width = "4px"; 
  94. height = "30px"; 
  95. borderRadius = "4px"; 
  96. background = fgm.getRanColor(); 
  97. }; 
  98. document.body.appendChild(oEntity); 
  99. oEntity.timer = setInterval(function() { 
  100. oEntity.style.top = oEntity.offsetTop - 20 + "px"; 
  101. if(oEntity.offsetTop <= param.y) { 
  102. clearInterval(oEntity.timer); 
  103. document.body.removeChild(oEntity); 
  104. (function() { 
  105. //在50-100之间随机生成碎片 
  106. //由于IE浏览器处理效率低, 随机范围缩小至20-30 
  107. //自动放烟花时, 随机范围缩小至20-30 
  108. var len = (/msie/i.test(navigator.userAgent) || that.type == 2) ? fgm.randomRange(20, 30) : fgm.randomRange(50, 100)  
  109. for (i = 0; i < len; i++) { 
  110. oChip = document.createElement("div"); 
  111. with(oChip.style) { 
  112. position = "absolute"; 
  113. top = param.y + "px"; 
  114. left = param.x + "px"; 
  115. width = "4px"; 
  116. height = "4px"; 
  117. overflow = "hidden"; 
  118. borderRadius = "4px"; 
  119. background = fgm.getRanColor();  
  120. }; 
  121. oChip.speedX = fgm.randomRange(-20, 20); 
  122. oChip.speedY = fgm.randomRange(-20, 20); 
  123. oFrag.appendChild(oChip); 
  124. aChip[i] = oChip 
  125. }; 
  126. document.body.appendChild(oFrag); 
  127. timer = setInterval(function() { 
  128. for(i = 0; i < aChip.length; i++) { 
  129. var obj = aChip[i]; 
  130. with(obj.style) { 
  131. top = obj.offsetTop + obj.speedY + "px"; 
  132. left = obj.offsetLeft + obj.speedX + "px"; 
  133. }; 
  134. obj.speedY++; 
  135. (obj.offsetTop < 0 || obj.offsetLeft < 0 || obj.offsetTop > document.documentElement.clientHeight || obj.offsetLeft > document.documentElement.clientWidth) && (document.body.removeChild(obj), aChip.splice(i, 1)) 
  136. }; 
  137. !aChip[0] && clearInterval(timer); 
  138. }, 30) 
  139. })() 
  140. } 
  141. }, 30) 
  142. } 
  143. }; 
  144. fgm.on(window, "load", function() { 
  145. var oTips = document.getElementById("tips"); 
  146. var aBtn = oTips.getElementsByTagName("a"); 
  147. var oFireWorks = new FireWorks(); 
  148. fgm.on(oTips, "click", function(event) { 
  149. var oEvent = event || window.event; 
  150. var oTarget = oEvent.target || oEvent.srcElement; 
  151. var i = 0; 
  152. if(oTarget.tagName.toUpperCase() == "A") { 
  153. for(i = 0; i < aBtn.length; i++) aBtn[i].className = ""; 
  154. switch(oTarget.id) { 
  155. case "manual": 
  156. oFireWorks.type = 1; 
  157. break; 
  158. case "auto": 
  159. oFireWorks.type = 2; 
  160. break; 
  161. case "stop": 
  162. oFireWorks.type = 0; 
  163. break; 
  164. } 
  165. oFireWorks.initialize(); 
  166. oTarget.className = "active"; 
  167. oEvent.stopPropagation ? oEvent.stopPropagation() : oEvent.cancelBubble = true 
  168. } 
  169. }); 
  170. }); 
  171. fgm.on(document, "contextmenu", function(event) { 
  172. var oEvent = event || window.event; 
  173. oEvent.preventDefault ? oEvent.preventDefault() : oEvent.returnValue = false 
  174. }); 
  175. </script> 
  176. </head> 
  177. <body> 
  178. <div id="tips"><a id="manual" href="javascript:;">手动放烟花</a><a id="auto" href="javascript:;">自动放烟花</a><a id="stop" href="javascript:;">停止放烟花</a></div> 
  179. <p>由于浏览器渲染能力有限, 当浏览器为IE或选择自动放烟花时, 随机生成的烟花碎片范围自动调整至20-30</p> 
  180. <div id="copyright">建议使用Firefox, Chrome浏览器预览效果. By — Ferris, QQ:21314130</div> 
  181. </body> 
  182. </html> 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选