jQuery 实现 跑马灯 和 大乐透

内容介绍

  jQuery 实现 跑马灯大乐透

一、跑马灯
1、效果图

在这里插入图片描述

2、 实现代码
<!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">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <title>Document</title>
    <style>
        .select-name-wrapper {
            width: 350px;
            height: 350px;
            position: relative;
        }
        
        .select-name-wrapper>.inner-wrapper>div {
            width: 100px;
            height: 100px;
            text-align: center;
            position: absolute;
            line-height: 100px;
            border: 1px solid white;
            background-color: orangered;
        }
        
        select-name-wrapper .content1 {
            top: 0;
            left: 0;
        }
        
        .select-name-wrapper .content2 {
            top: 0;
            left: 100px;
        }
        
        .select-name-wrapper .content3 {
            top: 0;
            left: 200px;
        }
        
        .select-name-wrapper .content4 {
            top: 100px;
            left: 200px;
        }
        
        .select-name-wrapper .content5 {
            top: 200px;
            left: 200px;
        }
        
        .select-name-wrapper .content6 {
            top: 200px;
            left: 100px;
        }
        
        .select-name-wrapper .content7 {
            top: 200px;
            left: 0;
        }
        
        .select-name-wrapper .content8 {
            top: 100px;
            left: 0;
        }
        
        .select-name-wrapper .btn {
            width: 100px;
            height: 100px;
            text-align: center;
            line-height: 100px;
            border: 1px solid white;
            background-color: orangered;
            position: absolute;
            top: 100px;
            left: 100px;
            cursor: pointer;
        }
        
        .on {
            background-color: yellow !important;
        }
    </style>

</head>

<body>
    <div class="select-name-wrapper">
        <div class="inner-wrapper">
            <div class=" list content1 on">1</div>
            <div class=" list content2">2</div>
            <div class=" list content3">3</div>
            <div class=" list content4">4</div>
            <div class=" list content5">5</div>
            <div class=" list content6">6</div>
            <div class=" list content7">7</div>
            <div class=" list content8">8</div>
        </div>
        <div class="btn" onselectstart="return false">点击</div>
    </div>
    <script>
        let index = 0;
        let timer = null;
        $('.select-name-wrapper .btn').on('click', function() {
            let num = 3000 + Math.random() * 3000;

            if (timer) {
                clearInterval(timer)
            }
            timer = setInterval(function() {
                index = ++index % $('.list').length
                num -= 200;
                if (num < 200) {
                    clearInterval(timer);
                    alert($('.list.on').html())
                    return
                }
                $('.list')
                    .eq(index)
                    .addClass('on')
                    .siblings('.on')
                    .removeClass('on')
            }, 50)
        });
    </script>
</body>

</html>
二、大乐透
1、效果图

在这里插入图片描述

2、 实现代码
<!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">
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <title>Document</title>
    <style>
        .redBall {
            height: 100px;
            width: 100px;
            font-size: 50px;
            line-height: 100px;
            text-align: center;
            background-color: red;
            float: left;
            border-radius: 50%;
            margin: 10px;
        }
        
        .blueBall {
            height: 100px;
            width: 100px;
            font-size: 50px;
            line-height: 100px;
            text-align: center;
            background-color: blue;
            float: left;
            border-radius: 50%;
            margin: 10px;
        }
        
        .ball-wrapper button {
            height: 30px;
            width: 100px;
            cursor: pointer;
            font: 16px;
            outline: none;
            border: none;
            border-radius: 5px;
            background-color: blue;
            color: #fff;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="ball-wrapper">
        <ul class="red-wrapper">
            <div class="redBall">1</div>
            <div class="redBall">2</div>
            <div class="redBall">3</div>
            <div class="redBall">4</div>
            <div class="redBall">5</div>
        </ul>
        <ol class="blue-wrapper">
            <div class="blueBall">6</div>
            <div class="blueBall">7</div>
        </ol>
        <!-- <div class="redBall">1</div>
    <div class="redBall">2</div>
    <div class="redBall">3</div>
    <div class="redBall">4</div>
    <div class="redBall">5</div>
    <div class="blueBall">6</div>
    <div class="blueBall">7</div> -->
        <button class="start-btn">随机选号</button>
        <button class="end-btn">停止</button>
    </div>

    <script>
        let timer = null;

        function init() {
            initEvent()
        }

        function initEvent() {
            $('.start-btn').on('click', startBtnClick)
            $('.end-btn').on('click', endBtnClick)
        }

        function startBtnClick() {
            if (timer) {
                clearInterval(timer);
            }
            timer = setInterval(() => {
                let redArr = fillBall(32, 5)
                let blueArr = fillBall(16, 2)

                fillHtml('.redBall', redArr)
                fillHtml('.blueBall', blueArr)
            }, 50)
        }

        function endBtnClick() {
            clearInterval(timer);
        }

        function fillHtml(el, arr) {
            arr.forEach((item, index) => {
                $(el).eq(index).html(item);
            })
        }

        function fillBall(max, len) {
            let arr = [];
            for (let i = 0; i < len; i++) {
                let num = Math.floor(Math.random() * max + 1);
                if (arr.includes(num)) {
                    i--;
                    continue;
                }
                arr.push(num)
            }
            return arr;
        }

        // 初始化业务片段
        init()
    </script>
</body>

</html>
三、注

以上代码只作为演示案例,可自行排版样式或增加有趣的功能。

👇 👇 👇 👇 👇

比如:添加运行结果输出、修改筛选概率等


标签:jQuery,跑马灯,大乐透


更多演示案例,查看 案例演示


欢迎评论留言!

  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 44
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

人间小美味695

您的鼓励是我创作的动力,感谢!

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

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

打赏作者

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

抵扣说明:

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

余额充值