js实现随机点名表

一个简易的点名表

 - css样式

    <style>
        .box{
            width: 350px;
            height: 300px;
            background-color: antiquewhite;
            margin: 0 auto;
            text-align: center;
        }
        #names{
            width: 260px;
            height: 260px;
            line-height: 260px;
            background-color: aquamarine;
            margin: 0 auto;
            font-size: 32px;
            color: red;
        }
    </style>
    
 - body部分
<body> 

<div class="box">
        <div id="names">点名了!!</div>
        <button onclick="start()">开始</button>
        <button onclick="end()">停止</button>
    </div>
 - script部分
     <!-- 随机点名效果 -->
   <script>
       var names = document.getElementById('names');
       var timeId;
       //封装的事随机显示名字的代码
       function start(){
           //数组,存放学生的名字
           var arr = ['亚瑟','不知火舞','安琪拉','貂蝉','鲁班七号'];
           timeId = setInterval(function(){
               //随机从数组里去元素
               var num = parseInt(Math.random() *arr.length)
               names.innerHTML=arr[num];
           },200)
 
       }
      // 清除事件
       function end(){
               clearInterval(timeId);
           }
           
   </script>
   
</body>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值