简单版随机点名js

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 600px;
            height: 400px;
            margin: 100px auto;
            background: linear-gradient(#010059, #52437b, #ff7a8a, #fcf594);
        }

        .box .top {
            height: 100px;
            display: flex;
            justify-content: space-around;
            align-content: center;
        }

        .box #bottom {
            text-align: center;
            font-size: 50px;
            height: 300px;
            line-height: 300px;
            color: #fff;
        }

        .box .top button {
            outline: none;
            height: 100px;
            background-color: rgba(255, 255, 255, 0.0);
            border: none;
            color: #fff;
            font-size: 16px;
            transition: 1s;
        }

        .box .top button:hover {
            font-size: 25px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="top">
            <button id="begin">请选择英雄</button>
            <button id="end">全军出击</button>
        </div>
        <div id="bottom"></div>
    </div>
    <script>
        var begin = document.getElementById("begin");   //获取元素开始按钮
        var end = document.getElementById("end");       //获取停止按钮
        var add = document.getElementById("bottom");   //获取元素
        var arr = ["南充李二狗", "顶级男模夏梦生", "贵阳可达鸭", "成都鸭王汪东旭","吃啃卤猪脚的王丽菲","新疆霸主胡玉兰","知名退堂鼓表演家","不愿意透露姓名的网友"];
        add.innerHTML = arr[0];   //初始化数据
        var timer;    //因为清除定时器的时候不能放入一个函数,该变量用来存放定时器
        begin.onclick = function () {   //绑定开始事件
            clearInterval(timer);
            timer = setInterval(function () {   //定时器
                var i = Math.floor(Math.random() * arr.length);
                // console.log(i);
                add.innerHTML = arr[i];         //把数组里的元素动态赋值给div.add中
            }, 50)
        }
        end.onclick = function () {     //清楚定时
            clearInterval(timer);
        }

    </script>
</body>

</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值