Demo5 随机点名

随机点名
需求:分为上下两个部分,上方为显示区域,下方为控制区域 显示区域显示基地所有成员的工号和姓名 控制区域由开始和结束两个按钮组成 点击开始按钮,显示区域里的内容开始滚动,点击结束按钮,内容滚动停止,随机显示一位成员的工号和姓名
实现原理:根据对显示区域添加节点等使数据导入,再通过间隔计时器定时执行Math.random获取随机值,通过给开始按钮点击事件绑定计时器,结束按钮绑定清除计时器实现

<!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;
            box-sizing: border-box;
        }
        .box{
            width: 880px;
            height: 600px;
            margin: 100px auto;
            border-radius: 20px;
            border: 2px solid wheat;
        }
        #top{
            width: 100%;
            height: 500px;
            border-bottom: 1px solid wheat;
        }
        #bot{
            width: 100%;
            height: 100px;
        }
        #bot button{
            margin: 10px 160px;
            height: 80px;
            width: 100px;
            border-radius: 10px;
            border: 1px solid wheat;
            line-height: 80px;
        }
        #bot button:hover{
            background-color: #fc5531;
        }
        .listchild{
            width: 150px;
            height: 60px;
            margin: 20px 34px;
            float: left;
            text-align: center;
            line-height: 60px;
            border: 1px solid wheat;
        }
    </style>
</head>
<body>
    <div class="box">
        <div id="top"></div>
        <div id="bot">
            <button id="start">开始</button>
            <button id="end">结束</button>
        </div>
    </div>
</body>
<script>
    var point=document.getElementById("top")
    // console.log(point)
    var list=[ 
    "01010101xxx","01010102xxx","01010103xxx","01010104xxx",
    "01010105xxx","01010106xxx","01010107xxx","01010108xxx",
    "01010109xxx","01010110xxx","01010111xxx","01010112xxx",
    "01010113xxx","01010114xxx","01010115xxx","01010116xxx",
    "01010117xxx","01010118xxx","01010119xxx","01010120xxx",
    ]

    for(let i=0;i<list.length;i++){
        var childvar=document.createElement("div")
        childvar.className="listchild"
        childvar.innerHTML=list[i]
        point.appendChild(childvar)
    }

    var start=document.getElementById("start")
    start.onclick=function(){
        time=setInterval(function(){
        for(let o=0;o<point.children.length;o++){
            point.children[o].style.background=""
        }
        point.children[parseInt(Math.random()*point.children.length)].style.background="#fc5531"
        },100 )
    }
    
    var end=document.getElementById("end")
    end.onclick=function(){
        clearInterval(time)
    }
</script>
</html>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值