JavaScript 实现随机点名

效果要求:

分为上下两个部分,上方为显示区域,下方为控制区域。显示区域显示基地所有成员的工号和姓名,控制区域由开始和结束两个按钮组成。点击开始按钮,显示区域里的内容开始滚动,点击结束按钮,内容滚动停止,随机显示一位成员的工号和姓名。

实现原理:

创建一个数组储存成员的姓名学号等等

利用Math.round(Math.random() * (arrName.length-1))创建一个随机数,作为索引调用储存学员姓名的数组

代码展示:

html:

<!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>
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div class="container">
        <div class="name">
            <ul></ul>
        </div>
        <div class="btn">
            <button class="start">开始</button>
            <button class="over">停止</button>
        </div>
    </div>

    <script src="./index.js"></script>
</body>
</html>

css

*{
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
}

.container{
    width: 1200px;
    min-height: 500px;
    margin: 0 auto;
    border-radius: 10px;
    background-color: pink;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
}

.name ul{
    width: 1200px;
    display: flex;
    flex-wrap: wrap;
}

li{
    width: 100px;
    height: 25px;
    margin: 10px;
    border-radius: 5px;
    background-color: white;

    text-align: center;
    line-height: 25px;
    color: black;
}

.liIndex{
    background-color: black;
    color: white;
    border-radius: 5px;
}


.btn{
    margin-top: 100px;
    margin-bottom: 50px;
    width: 500px;
    display: flex;
    justify-content: space-between;
}

.start{
    width: 150px;
    height: 40px;
    border-radius: 10px;
    outline: none;
    cursor: pointer;

    font-size: 25px;
    line-height: 40px;
    text-align: center;
}

.over{
    width: 150px;
    height: 40px;
    border-radius: 10px;
    outline: none;
    cursor: pointer;

    font-size: 25px;
    line-height: 40px;
    text-align: center;
}

js

window.onload = function () {
    // 获取元素节点
    var name = document.getElementsByClassName("name")[0]
    var ul = name.getElementsByTagName("ul")[0]
    var start = document.getElementsByClassName("start")[0]
    var over = document.getElementsByClassName("over")[0]

    // 创建数组储存每个成员的名字
    // var arrName = []
    // 这里用数字代替名字
    var arrName = []
    for (var i = 1; i < 87; i++) {
        arrName.push(i + "号")
    }
    console.log(arrName)

    for (var i = 0; i < arrName.length; i++) {
        // 创建li标签
        var li = document.createElement("li")
        // 将成员名字输入li中
        li.innerHTML = arrName[i]
        // 将li标签插入ul标签
        ul.appendChild(li)
    }

    // var tu = document.getElementsByClassName("tu")
    var lis = ul.getElementsByTagName("li")

    start.onclick = function () {
        // 创建抽取名字时的循环计时器
        var startTime = setInterval(function () {
            // 设置随机数
            for(var i =0;i<arrName.length;i++){
                lis[i].className = ""
            }
            var random = Math.round(Math.random() * (arrName.length-1)) 

            lis[random].className = "liIndex"

            // 设置停止按钮的清除计时器
            over.onclick = function(){
                clearInterval(startTime)
            }
        }, 100)
    }
}

效果展示:

随机点名

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

O_oregou

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

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

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

打赏作者

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

抵扣说明:

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

余额充值