Blogger Information
Blog 46
fans 0
comment 0
visits 34471
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示双色球
上草一方
Original
609 people have browsed it

实例演示双色球

代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>双色球</title>
  8. <style>
  9. .box {
  10. display: grid;
  11. grid-template-columns: repeat(auto-fill, 30px);
  12. grid-auto-rows: 30px;
  13. gap: 5px;
  14. }
  15. .box>div {
  16. border-radius: 50%;
  17. display: grid;
  18. place-items: center;
  19. background-color: red;
  20. color: white;
  21. box-shadow: 2px 2px 2px #666;
  22. }
  23. .box>div:last-of-type {
  24. background-color: blue;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box"></div>
  30. <script>
  31. // 红球:1-33=>6个,不重复,排序
  32. // 蓝球:1-16=>1个,可以和红球重复
  33. // 1.先取红球,放在一个中奖号数组中,排序
  34. // 2.然后把蓝球追加到数组中
  35. // 3.还要把中奖数组渲染到页面:dom
  36. // 临时数组:放红球
  37. let arr=[];
  38. // 中奖数组,最后应该有7个数
  39. let res=[];
  40. //1.生成1-33个红球号
  41. for(let i=1;i<34;i++){
  42. arr.push(i);
  43. }
  44. console.log(arr);
  45. // 2.从33个红球中取出6个
  46. for (let i=0; i<6; i++){
  47. // arr的下标取值范围:0-32
  48. // math.radom():0-1之间的小数
  49. // console.log(Math.random());
  50. // console.log(Math.random()*33);
  51. // 这样就得到一个0-33之间的小数,再给它作一个取整的操作,向上取整,就可以得到1-33中的任何一个整数
  52. // console.log(Math.floor(Math.random()*arr.length));
  53. let index=Math.floor(Math.random()*arr.length);
  54. res.push(arr[index]);
  55. arr.splice(index,1);
  56. }
  57. console.log(res);
  58. //排序
  59. res.sort((a, b)=>a - b);
  60. console.log(res);
  61. // 3.生成一个蓝球,并追加到中奖数组中
  62. let blue=Math.floor(Math.random()*16)+1;
  63. console.log(blue);
  64. res.push(blue);
  65. console.log(res);
  66. // 4.将中奖号码显示到页面中
  67. const box=document.querySelector('.box');
  68. res.forEach(item=>{
  69. const ball=document.createElement('div');
  70. ball.textContent=item;
  71. box.append(ball);
  72. })
  73. </script>
  74. </body>
  75. </html>

效果图如下:

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post