js随机点名

1.随机整数

Math.floor()向下取整,Math.random()随机数0~1,

   function getRandom(min,max){

             return Math.floor(Math.random()*(max-min+1))+min

        }

可以获取随机整数

2.定时器

setInterval(function() { },interave)间隔定时器

settimeout(function() { },interave)倒计时定时器

3.实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;padding: 0;
            list-style: none;
        }
        div{
            width: 500px;
            margin: 0 auto;
        }
       h1{
           height: 70px;
            background-color: honeydew;
       }
       #list .seclect{
          background-color: pink;
      }
      #list .active{
          background-color: goldenrod;
      }
      #list li{
          float: left;
          margin:5px 5px;
          width: 80px;
          text-align: center;
          line-height: 40px;
          height: 40px;
          background-color: gray;
      }
    </style>
</head>
<body>
    <div>
        <h1 id="h1">被选中</h1>
        <button id="btn">开始</button>
        <button id="btl">暂停</button>
        <ul id="list"></ul>
    </div>
    <script>
        var arr=["张三","李四","王五","克六","乔治","威少","杜兰特","詹姆斯","保罗","库里","欧文","汤普森"]
    
        for(var i=0;i<arr.length;i++){
            var oli=document.createElement("li")
             oli.innerHTML=`${arr[i]}`
             list.append(oli)
        }
      var lp
       var oli1=document.querySelectorAll("li")
       console.log(oli1)
        btn.onclick=function(){
            clearInterval(lp)
            timl()
            
        }
       
        function timl(){
          lp= setInterval(function() { 
    
               var lac=document.querySelector("li.active")
               lac && lac.classList.remove("active")
             //    避免重复选择
               var kk=document.querySelectorAll("li:not(.seclect)")
               var index = getRandom(0,kk.length-1)
                kk[index].classList.add("active")
            }, 100);
        }
        function getRandom(min,max){
             return Math.floor(Math.random()*(max-min+1))+min
        }
        btl.onclick=function(){
            clearInterval(lp)
            var selet=document.querySelector("li.active")
            selet.classList.add("seclect")
            selet.classList.remove("active")
            h1.innerHTML+=" " +selet.innerHTML
        }

  </script>
</body>
</html>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值