首页 > 语言 > JavaScript > 正文

JS数字抽奖游戏实现方法

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

这篇文章主要介绍了JS数字抽奖游戏实现方法,可实现按下回车键出现随机数字切换的效果,涉及时间与随机数的相关操作技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了JS数字抽奖游戏实现方法。分享给大家供大家参考。具体实现方法如下:

 

 
  1. <!doctype html> 
  2. <html> 
  3. <head> 
  4. <meta charset="utf-8"> 
  5. <title>新年网页抽奖程序</title> 
  6. <style type="text/css"> 
  7. * {margin:0; padding:0;} 
  8. ul,li {list-style-type:none;} 
  9. body {overflow:hidden;} 
  10. #back {width:100%; height:100%; 
  11. background:#f5f5f5; position:absolute; z-index:1; 
  12. } 
  13. #box {width:360px; height:100px; 
  14. position:absolute; z-index:3; top:50%; left:50%; 
  15. margin-top:-50px; margin-left:-180px; text-align:center; 
  16. } 
  17. #box1,#box2,#box3 {width:100px; height:100px; 
  18. line-height:100px; 
  19. float:left; background:#321c24;  
  20. border:10px #321c24 solid; 
  21. border-radius:50%; position:relative; overflow:hidden; 
  22. } 
  23. #box1 ul,#box2 ul,#box3 ul {color:#fff; font-size:68px;  
  24. font-family:"Arial Black"; text-align:center; 
  25. width:100px; height:100px; line-height:100px; 
  26. position:absolute; top:0; left:0; 
  27. } 
  28. #box1 ul li,#box2 ul li,#box3 ul li { 
  29. width:100px; height:100px; 
  30. background:red; border-radius:50%; 
  31. } 
  32. </style> 
  33. <script type="text/javascript"> 
  34. var AIR = { 
  35. $: function (id) 
  36. { 
  37. return typeof id === "string" ? document.getElementById(id) : id; 
  38. },  
  39. $: function (elem, oParent) 
  40. { 
  41. return (oParent || document).getElementsByTagName(elem); 
  42. }, 
  43. addEvent: function (oElement, sEvent, fnHandler)  
  44. { 
  45. oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)  
  46. }, 
  47. removeEvent: function (oElement, sEvent, fnHandler)  
  48. { 
  49. oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler) 
  50. },  
  51. getElementClient: function (){ 
  52. var arr = []; 
  53. if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth){ 
  54. arr.push(document.documentElement.clientWidth); 
  55. arr.push(document.documentElement.clientHeight); 
  56. return arr; 
  57. } 
  58. }, 
  59. getStyle: function (obj, attr) 
  60. { 
  61. return parseFloat(obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr]) 
  62. }, 
  63. startMove: function (obj, pos, onEnd) 
  64. { 
  65. clearInterval(obj.timer); 
  66. var _this = this; 
  67. obj.timer = setInterval(function () 
  68. { 
  69. _this.doMove(obj, pos, onEnd) 
  70. }, 30)  
  71. }, 
  72. doMove: function (obj, pos, onEnd) 
  73. { 
  74. var iCurL = this.getStyle(obj, "left"); 
  75. var iCurT = this.getStyle(obj, "top"); 
  76. var iSpeedL = (pos.left - iCurL) / 5; 
  77. var iSpeedT = (pos.top - iCurT) / 5; 
  78. iSpeedL = iSpeedL > 0 ? Math.ceil(iSpeedL) : Math.floor(iSpeedL); 
  79. iSpeedT = iSpeedT > 0 ? Math.ceil(iSpeedT) : Math.floor(iSpeedT); 
  80. if (pos.left == iCurL && pos.top == iCurT) 
  81. { 
  82. clearInterval(obj.timer); 
  83. onEnd && onEnd() 
  84. } 
  85. else 
  86. { 
  87. obj.style.left = iCurL + iSpeedL + "px"; 
  88. obj.style.top = iCurT + iSpeedT + "px";  
  89. } 
  90. } 
  91. } 
  92. function Draw (obj, num) 
  93. { 
  94. this.obj = obj; 
  95. this.num = num; 
  96. this.data = []; 
  97. this.result = []; 
  98. this.show = 0; 
  99. this.btn = true; 
  100. this.timer = true; 
  101. this.h = 0; 
  102. this.uh = 0; 
  103. this.initialize();  
  104. } 
  105. Draw.prototype = { 
  106. initialize: function () 
  107. { 
  108. this.createArr (); 
  109. this.createElement (); 
  110. this.closeEvent (); 
  111. this.startDraw ();  
  112. }, 
  113. createElement: function () 
  114. { 
  115. for(var j=0; j<this.obj.length; j++){  
  116. var ul = document.createElement("ul"); 
  117. for(var i=0; i<10; i++){ 
  118. var li = document.createElement("li"); 
  119. li.innerHTML = i; 
  120. ul.appendChild(li)  
  121. }  
  122. this.obj[j].appendChild(ul); 
  123. this.obj[j].btn = true; 
  124. AIR.$("ul",this.obj[j])[0].innerHTML += AIR.$("ul",this.obj[j])[0].innerHTML;  
  125. }  
  126. var UL = AIR.$("ul",this.obj[0])[0]; 
  127. this.h = AIR.getStyle(AIR.$("li",UL)[0],"height"); 
  128. this.uh = AIR.$("li",UL).length * this.h 
  129. }, 
  130. randomSort: function (a, b) { 
  131. return Math.random()>.5 ? -1 : 1; 
  132. }, 
  133. createArr: function () 
  134. { 
  135. for(var i=0; i<this.num+1; i++){ 
  136. this.data.push(i);  
  137. }  
  138. this.data.sort(this.randomSort);  
  139. }, 
  140. closeEvent: function () 
  141. { 
  142. document.onmousedown=document.onmousemove=document.oncontextmenu=function() 
  143. { 
  144. return false;  
  145. }  
  146. }, 
  147. startDraw: function () 
  148. { 
  149. var _this = this; 
  150. document.onkeyup = function ( ev ) 
  151. { 
  152. var ev = ev || window.event; 
  153. if(ev.keyCode == 13 || ev.keyCode == 32){ 
  154. if(_this.btn && _this.timer){ 
  155. if(_this.obj[_this.obj.length-1].btn){ 
  156. _this.Play (); 
  157. _this.btn = !_this.btn; 
  158. _this.timer = !_this.timer;  
  159. }  
  160. }else{ 
  161. if(_this.obj[_this.obj.length-1].btn){ 
  162. _this.Stop (); 
  163. _this.btn = !_this.btn; 
  164. _this.timer = !_this.timer;  
  165. } 
  166. } 
  167. return false; 
  168. }else{ 
  169. return false;  
  170. } 
  171. } 
  172. }, 
  173. Play: function () 
  174. { 
  175. if(this.timer && this.btn){ 
  176. var t = 0; 
  177. for(var i=0; i<this.obj.length; i++){ 
  178. this.obj[i].btn = false; 
  179. this.playTimer (this.obj[i],t);  
  180. t += 1500; 
  181. } 
  182. }else{ 
  183. return false;  
  184. } 
  185. }, 
  186. playTimer: function (obj,t) 
  187. { 
  188. var _this = this; 
  189. setTimeout(function(){ 
  190. _this.Move (obj); 
  191. },t)  
  192. }, 
  193. Del: function (a) 
  194. { 
  195. for(var i=0; i<this.data.length; i++){ 
  196. if(a == this.data[i]){ 
  197. this.data.splice(i,1);  
  198. }  
  199. }  
  200. }, 
  201. Stop: function () 
  202. { 
  203. if(!this.timer && !this.btn){ 
  204. var n = this.num + 1; 
  205. var r = this.data[Math.floor(Math.random() * (0-n) + n)]; 
  206. this.show = r; 
  207. this.Del (r); 
  208. r = r.toString().split(""); 
  209. var c = this.obj.length - r.length; 
  210. if(r.length < this.obj.length){ 
  211. for(var i=0; i<c; i++){ 
  212. r.unshift(0)  
  213. }  
  214. } 
  215. this.result = r;  
  216. //document.title = r+" : "+this.data;  
  217. var t = 0; 
  218. for(var i=0; i<this.obj.length; i++){ 
  219. this.obj[i].btn = false; 
  220. this.obj[i].index = i; 
  221. this.obj[i].num = this.result[this.obj[i].index]; 
  222. this.stopTimer (this.obj[i],t);  
  223. t += 1500; 
  224. } 
  225. } 
  226. }, 
  227. stopTimer: function (obj,t) 
  228. { 
  229. var _this = this; 
  230. setTimeout(function(){ 
  231. _this.showResult (obj); 
  232. },t) 
  233. }, 
  234. showResult: function (obj) 
  235. {  
  236. var _this = this; 
  237. this.timer = true; 
  238. this.btn = true; 
  239. obj.btn = false; 
  240. obj.vh = -obj.num * this.h; 
  241. obj.timeOut = setInterval(function(){ 
  242. obj.speed -= 1; 
  243. if(obj.speed == 1){ 
  244. clearInterval(obj.timeOut);  
  245. clearInterval(obj.timer); 
  246. obj.timer = setInterval(function(){ 
  247. if(obj.ul.offsetTop >= obj.vh){ 
  248. clearInterval(obj.timer); 
  249. AIR.startMove(obj.ul,{left:0,top:obj.vh},function(){ 
  250. obj.btn = true;  
  251. var set = true; 
  252. for(var i=0; i<_this.obj.length; i++){ 
  253. if(!_this.obj[i].btn){ 
  254. set = false;  
  255. } 
  256. } 
  257. if(set){ 
  258. _this.Open(_this.show)  
  259. } 
  260. }); 
  261. } 
  262. obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px";  
  263. },30); 
  264. } 
  265. },100);  
  266. }, 
  267. Open: function (num) 
  268. { 
  269. document.title += " "+ num; 
  270. }, 
  271. Move: function (obj) 
  272. { 
  273. var _this = this; 
  274. var obj = obj; 
  275. obj.btn = false; 
  276. obj.timer = null; 
  277. obj.speed = 1; 
  278. obj.ul = AIR.$("ul",obj)[0]; 
  279. obj.ul.style.height = this.uh +"px"; 
  280. obj.timer = setInterval(function(){ 
  281. if(obj.ul.offsetTop > 0){ 
  282. obj.ul.style.top = -(_this.uh/2) +"px"; 
  283. } 
  284. obj.ul.style.top = obj.ul.offsetTop + obj.speed +"px";  
  285. },30); 
  286. obj.timeOut = setInterval(function(){ 
  287. obj.speed += 1; 
  288. if(obj.speed == 30){ 
  289. clearInterval(obj.timeOut); 
  290. obj.btn = true;  
  291. } 
  292. },300)  
  293. } 
  294. } 
  295. var initialize = function () 
  296. { 
  297. new Draw ([AIR.$("box1"),AIR.$("box2"),AIR.$("box3")],100); 
  298. reSize (); 
  299. } 
  300. var reSize = function () 
  301. { 
  302. var v = AIR.getElementClient(); 
  303. AIR.$("img",AIR.$("back"))[0].width = v[0]; 
  304. AIR.$("img",AIR.$("back"))[0].height = v[1];  
  305. } 
  306. AIR.addEvent(window,"load",initialize); 
  307. AIR.addEvent(window,"resize",reSize); 
  308. </script> 
  309. </head> 
  310. <body> 
  311. <div id="box"> 
  312. <div id="box1"></div> 
  313. <div id="box2"></div> 
  314. <div id="box3"></div> 
  315. <div style="clear:both"></div> 
  316. </div> 
  317. <div id="back"> 
  318. <img src="images/20153291274950386.jpg" /> 
  319. </div> 
  320. <div id="showback">100</div> 
  321. </body> 
  322. </html> 

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

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

图片精选