JS实现的随机点名的模型

先看看运行效果



再看源代码
  1. <!-- 
  2.   下面的代码是我做的我们班每次上课前的一个随机点名的简单模型
  3.   本人觉得写的不是很好 希望靠各位大侠的想象力和精湛的技术改进下 谢谢了
  4. -->
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" c />
  9. <title>幸运星 就是你</title>
  10. <style>
  11. td {
  12.         background-color: #FFFFFF;
  13.         border: 1px solid #009933;
  14.         font-size: 50pt;
  15.         height: 70px;
  16.         width: 75px;
  17.         text-align: center;
  18. }
  19. .button {
  20.         background-color: transparent;
  21.         border: 1px solid #009933;
  22.         font-size: 20pt;
  23.         color: #009933;
  24.         height: 40px;
  25.         width: 100px;
  26. }
  27. .sp {
  28.         background-color: #FFFFFF;
  29. }
  30. .total {
  31.         border: 1px solid #009933;
  32.         border-bottom: 1px solid #009933;
  33.         border-top: 0px;
  34.         border-left: 0px;
  35.         border-right: 0px;
  36.         background-color: transparent; /* 背景色透明 */
  37.         text-align: center;
  38.         font-size: 14px;
  39.         color: green;
  40.         width: 50px;
  41. }
  42. .f {
  43.         font-size: 14px;
  44.         color: #009933;
  45. }
  46. </style>
  47. <script type="text/javascript" language="javascript">
  48.   var tds = document.getElementsByTagName("td");//获得所有的td
  49.   var time;//每跳一格运行的时间 1-50ms之间
  50.   var count = 0;//每个td对应的下标
  51.   var totalTime;//每跳一格运行时间的总和
  52.   var maxTime;//maxTime是每次运行的最大时间 在1000到6000之间 随机的
  53.   var flag;//用来开始和停止的标识 1表示开始 0表示结束
  54.   
  55.   //开始
  56.   function begin()
  57.   {
  58.     flag = 1;
  59.     maxTime = Math.floor(Math.random()*3000) + Math.floor(Math.random()*2000) + 1000;
  60.     totalTime = 0;//每次运行时 将前一次运行的总时间 清零
  61.     changeColor();
  62.   }
  63.   
  64.   //结束
  65.   function end() 
  66.     {   
  67.         flag = 0;
  68.     } 
  69.   
  70.   function changeColor()
  71.   {   
  72.       time = Math.floor(Math.random()*50) + 1;//本行放在这里 则每次每格运行的时间是不一样的
  73.     if(count < tds.length)
  74.     { 
  75.       //下面这样写 主要是为了按1-16顺序选择 本人觉得不是很好 如果哪位大侠有更好的 欢迎指教
  76.       tds[count].style.color="#FF0DF0";
  77.       rollback(count);
  78.       if(count == 4)
  79.       {
  80.         count = 6;//输出6
  81.       } 
  82.       else if(count == 7)
  83.       {
  84.         rollback(count);
  85.         count = 8;//输出7
  86.       } 
  87.       else if(count == 9)
  88.       {
  89.         rollback(count);
  90.         count = 10;//输出8
  91.       } 
  92.       else if(count == 11)
  93.       {
  94.         rollback(count);
  95.         count = 15;//输出9
  96.       }
  97.       else if(count == 16)
  98.       {
  99.         rollback(count);
  100.         count = 14;//输出10
  101.       }
  102.       else if(count == 15)
  103.       {
  104.         rollback(count);
  105.         count = 13;//输出11
  106.       }
  107.       else if(count == 14)
  108.       {
  109.         rollback(count);
  110.         count = 12;//输出12
  111.       }
  112.       else if(count == 13)
  113.       {
  114.         rollback(count);
  115.         count = 11;//输出13
  116.       }
  117.       else if(count == 12)
  118.       {
  119.         rollback(count);
  120.         count = 9;//输出14
  121.       }
  122.       else if(count == 10)
  123.       {
  124.         rollback(count);
  125.         count = 7;//输出15
  126.       }
  127.       else if(count == 8)
  128.       {
  129.         rollback(count);
  130.         count = 4;//输出16
  131.       }
  132.       else if(count == 5)//当显示的是16时 转头重新从1开始显示
  133.       {
  134.         count = -1;
  135.       }
  136.     }
  137.     countcount = count + 1;    
  138.     
  139.     totalTimetotalTime = totalTime + time;//每次运行的时间累加
  140.     
  141.     if(totalTime < maxTime)//如果累计运行的时间小于maxTime 则继续运行 否则停止
  142.     {
  143.       if (flag == 0)   
  144.             {
  145.         return false;
  146.       }
  147.       setTimeout("changeColor()", time);        
  148.     }   
  149.   }
  150.   
  151.   //rollback方法是为了将不是当前的数字的颜色恢复为以前的颜色 以示区别
  152.   function rollback(s)
  153.   {
  154.     for(var i = 0; i < tds.length; i++)
  155.     {
  156.       if(i != s)
  157.       {
  158.         tds[i].style.color="#000000";
  159.       }
  160.     } 
  161.   }
  162. </script>
  163. </head>
  164. <body>
  165. <form name="luck">
  166. <table id="luckboy" border="0" align="center" cellpadding="0"
  167.   cellspacing="1" style="position: relative; left: 0px; top: 30px">
  168.   <tr>
  169.     <td>1</td>
  170.     <td>2</td>
  171.     <td>3</td>
  172.     <td>4</td>
  173.     <td>5</td>
  174.   </tr>
  175.   <tr>
  176.     <td>16</td>
  177.     <td colspan="3" rowspan="3" class="sp"><img
  178.       src="ImgE_250x250.png" align="middle" /><!-- 你的图片 --></td>
  179.     <td>6</td>
  180.   </tr>
  181.   <tr>
  182.     <td>15</td>
  183.     <td>7</td>
  184.   </tr>
  185.   <tr>
  186.     <td>14</td>
  187.     <td>8</td>
  188.   </tr>
  189.   <tr>
  190.     <td>13</td>
  191.     <td>12</td>
  192.     <td>11</td>
  193.     <td>10</td>
  194.     <td>9</td>
  195.   </tr>
  196. </table>
  197. <br />
  198. <br />
  199. <br />
  200. <div align="center"><input type=button name="start" value="开 始"
  201.   class="button" onclick=begin();> <input type=button
  202.   name="over" value="结 束" class="button" onclick=end();></div>
  203. </form>
  204. </body>
  205. </html>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值