jquery 实现抽奖小程序

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .con{
            width: 200px;
            margin: 100px auto;
            padding: 10px 0;
            color: white;
            font-weight: bold;
            font-family: "楷体";
            text-align: center;
            border: groove 4px gray;
            background-color: red;
        }

        img{
            height: 30px;
            vertical-align: middle;
            border-radius: 50%;
        }

        /*设置两个相邻元素,垂直居中对齐的方式由哪些??*/

        .btns button{
            margin-top: 12px;
            padding: 4px;
            font-size: 12px;
            border: none;
            outline: none;
            background: palegreen;
            cursor: pointer;
        }

        .hid{
            display: none;
        }
    </style>
</head>
<body>
    <div class="con">
        <img src="./images/9.ico" alt="" class="pic">
        <span class="name">王雷</span>
        <div class="btns"><button id="start">开始抽奖</button><button id="stop" class="hid">结束抽奖</button></div>
    </div>
    <script src="./js/jquery-3.5.0.js"></script>
    <script>
        var arr = ["小名","小亮","小红","老王","老孙","老李"];
        var picArr = ["./images/9.ico","./images/10.ico","./images/11.ico","./images/12.ico","./images/13.ico","./images/14.ico"];

        //定义一个变量,用来接收计时器
        var timer = null;

        //点击开始抽奖  随机切换头像和姓名
        $("#start").click(function(){
            //出现结束抽奖按钮
            $("#stop").removeClass("hid").siblings().addClass("hid");
            timer = setInterval(function () {
               //获取随机索引
               var ranIdx = parseInt(Math.random() * arr.length);
               //通过随机索引  获取这个随机索引对应的姓名 和  头像   并将其回显到页面对应的位置
               $(".pic").prop("src",picArr[ranIdx]);
               $(".name").html(arr[ranIdx]);
           },100)
        })

        //点击结束抽奖 显示中奖人员
        $("#stop").click(function () {
            //出现开始抽奖按钮
            $("#start").removeClass("hid").siblings().addClass("hid");
            //清除计时器
            clearInterval(timer);
        })
    </script>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大闸蟹~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值