js 随机点名

需求分析
在上方块中展示全员名单
随机点名之后出现滚动条,出现人员性名
点击结束之后出现选中人员

原理实现
布局上方人员名单以及下方按钮
点击点名之后人员名单的display消失,滚动display显现,利用数组存贮人员名单,产生随机数,将数组【随机数】打印至滚动条中
结束时,计时器结束,保留人员名字

效果展示

随机点名

<!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;
        }
        .mingzi{
            height: 500px;
            width: 100%;
            display: flex;
            flex-wrap: wrap;
            background-color: antiquewhite;
        }
        ul{
            display: flex;
            flex-wrap: wrap;
        }
        .mingzi li{
            height: 100px;
            width: 200px;
            margin-left: 50px;
            margin-top: 50px;
            text-align: center;
            line-height: 100px;
            background-color: aqua;
            display: block;
        }
        .bottom{
            width: 100%;
            height: 100px;
            display: flex;
            font-size: 30px;
            font-weight: 500;
            line-height: 100px;
            text-align: center;
            background-color: blanchedalmond;
        }
        .start{
            width: 200px;
            height: 100px;
            margin-left: 400px;
            background-color: gray;
        }
        .end{
            height: 100px;
            width: 200px;
            background-color: gray;
            margin-left: 300px;
        }
        .checked{
            position: absolute;
            left: 650px;
            top: 200px;
            height: 100px;
            width: 200px;
            text-align: center;
            color: white;
            background-color: brown;
            line-height: 100px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="mingzi">
        <ul>
            <li>02010101</li>
            <li>02010102</li>
            <li>02010103</li>
            <li>02010104</li>
            <li>02010105</li>
            <li>02010106</li>
            <li>02010107</li>
            <li>02010108</li>
            <li>02010109</li>
            <li>02010110</li>
            <li>02010111</li>
            <li>02010112</li>
        </ul>
    </div>
    <div class="bottom">
        <div class="start">开始</div>
        <div class="end">结束</div>
    </div>
    <div class="checked"></div>
    <script>
        var start=document.querySelector('.start');
        var end=document.querySelector('.end');
        var ul=document.querySelector('ul');
        var checked=document.querySelector('.checked');
        var shuzu=document.getElementsByTagName('li');
        var name;

        start.onclick=function(){
            ul.style.display="none";
            checked.style.display="block";
            run=setInterval(function(){
                var number=Math.floor(Math.random()*12);
                name=shuzu[number].textContent
                checked.innerHTML=shuzu[number].textContent
            },100)
        }

        end.onclick=function(){
            clearInterval(run);
            checked.innerHTML="选中的是"+name
        }
    </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优降宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值