JQuery实现随机抽奖

效果演示

请添加图片描述

具体实现

<!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>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(function () {
            //3、处理按钮点击前和点击后是否可用
            $("#start").prop("disabled", false);
            $("#end").prop("disabled", true);


            // 1、给开始按钮绑定单击事件
                // 1.1 定义循环计时器
                // 1.2 切换小相框的src属性
                    // * 定义一个数组,存储图片资源路径
                    // * 生成随机数,数组角标

            var imgs = ["./image/test1.jpg",
                "./image/test2.jpg",
                "./image/test3.jpg"
            ];
            var id; //接收定时器返回的id值
            var index; //数组索引
            $("#start").click(function () {
                id = setInterval(function () {
                    //生成随机角标
                    index = Math.floor(Math.random() * 4);
                    /*
                    Math.floor()方法向下取整
                    Math.random()方法可以获取0.0到3.999...的任意值
                    */
                    //获取相框的src属性
                    $("#img1").prop("src", imgs[index]);

                    //处理按钮是否可用
                    $("#start").prop("disabled", true);
                    $("#end").prop("disabled", false);

                }, 20);
            })

            // 2、给结束按钮绑定单击事件
            // 2.1 停止计时器
            // 2.2 给大相框设置src属性

            $("#end").click(function () {
                clearInterval(id);
                $("#img2").prop("src", imgs[index]);
                //处理按钮是否可用
                $("#start").prop("disabled", false);
                $("#end").prop("disabled", true);
            });
        })
    </script>
</head>

<body>
    <!-- 小相框 -->
    <div style="border-style: dotted;width: 90px;height: 150px;">
        <img id="img1" src="./image/test1.jpg" style="width: 90px;height: 150px;">
    </div>
    <!-- 大相框 -->
    <div style="border-style:double; width:270px; height:450px">
        <img id="img2" src="./image/test1.jpg" style="width:270px; height:450px">
    </div>

    <!-- 开始按钮 -->
    <input id="start" type="button" value="点击开始" style="width: 150px;height: 100px;font-size: 30px;">
    <!-- 停止按钮 -->
    <input id="end" type="button" value="点击暂停" style="width: 150px;height: 100px;font-size: 30px;">
</body>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值