一个简单的线下转盘活动,指定记录,有增速度和减速

代码在此,请品尝

在线地址:JS Bin - Collaborative JavaScript Debugging

<!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>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .circle {
            width: 400px;
            height: 400px;
            border: 1px solid red;
            position: relative;
            margin: 50px auto;
        }

        .circle::after {
            content: "";
            display: block;
            width: 100%;
            height: 1px;
            background-color: red;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }

        .circle::before {
            content: "";
            display: block;
            height: 100%;
            width: 1px;
            background-color: red;
            position: absolute;
            left: 50%;
            top: 0;
            transform: translateX(-50%);
        }

        .item {
            position: absolute;
        }

        .arrow {
            width: 50px;
            height: 50px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -25px;
            margin-top: -25px;
            background-color: antiquewhite;
            border-radius: 100%;

        }

        .arrow::after {
            content: "";
            display: block;
            width: 10px;
            height: 40px;
            position: absolute;
            left: 50%;
            top: -40px;
            transform: translateX(-50%);
            background-color: antiquewhite;
        }

        .result {
            margin: 0 auto;
            display: flex;
            justify-content: center;
        }
    </style>
</head>

<body>
    <div id="result" class="result"></div>
    <div class="circle">
        <div class="item" style="left:30px;top:30px">
            <div>游园一场</div>
            <div>游园活动0%</div>
        </div>
        <div class="item" style="right:30px;top:30px">
            <div>果切</div>
            <div>果切活动89%</div>
        </div>
        <div class="item" style="right:30px;bottom:30px">
            <div>小礼品</div>
            <div>小礼品10%</div>
        </div>
        <div class="item" style="left:30px;bottom:30px">
            <div>谢谢</div>
            <div>感谢参与1%</div>
        </div>
        <div class="arrow" id="arrow"></div>
    </div>
    <div style="display: flex; justify-content: center;"><button id="start">开始游戏</button></div>
    <script>
        function getRandomNumber(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        function getTargetRoute() {
            // 设定中奖几率 果切活动89% 小礼品10% 感谢参与1% 游园活动0% (将游园活动的0几率排除)  
            const fruitChance = 89;
            const giftChance = 10;
            const thankChance = 1;
            // 计算总的中奖几率 
            const totalChance = fruitChance + giftChance + thankChance;
            // 生成一个0到totalChance-1之间的随机数  
            const randomNum = Math.floor(Math.random() * totalChance);
            //基础圈数
            const baseRoute = 360 * this.getRandomNumber(4, 10)
            // 根据随机数确定中奖结果  
            let result;
            let roate
            if (randomNum < fruitChance) {
                result = '果切';
                roate = baseRoute + this.getRandomNumber(10, 80) //不是0到90 为了防止落在边边,造成分歧

            } else if (randomNum < fruitChance + giftChance) {
                result = '小礼品';
                roate = baseRoute + this.getRandomNumber(100, 170)
            } else {
                result = '感谢参与';
                roate = baseRoute + this.getRandomNumber(280, 350)
            }
            return {
                result,
                roate
            }
        }
        let timer = null;
        let playing = false;
        function startGame(targetInfo) {
            const {
                result,
                roate
            } = targetInfo;
            //初始速度
            let speed = 1;
            //初始旋转位置
            let currentRoate = 0;
            document.querySelector("#result").innerHTML = `即将中奖的是:${result}`
            const dom = document.querySelector("#arrow");
            clearInterval(timer)
            playing = true;
            timer = setInterval(() => {
                currentRoate += speed;
                dom.style.transform = 'rotate(' + currentRoate + 'deg)';
                const percent = currentRoate / roate;
                if (percent < 0.7) {
                    speed += 0.1;
                    console.log("速度增加", speed)
                }
                else if (percent >= 0.7 && speed > 0.5) {
                    //speed > 0.5是用来兜底的,否则还没到目标节点
                    speed -= 0.2;
                    console.log("速度减少", speed)
                }
                if (currentRoate >= roate) {
                    clearInterval(timer);
                    playing = false;
                    alert(`中大奖了:${result}`)
                }
            }, 20)

        }
        document.querySelector("#start").onclick = function () {
            if (playing) {
                return;
            }
            //提前获取开奖结果,和旋转角度
            const targetInfo = getTargetRoute();
            //开始游戏
            startGame(targetInfo);
        }

    </script>
</body>

</html>

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-16 00:22:16       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-16 00:22:16       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-16 00:22:16       82 阅读
  4. Python语言-面向对象

    2024-05-16 00:22:16       91 阅读

热门阅读

  1. 【Linux深度学习笔记5.13(Apache)】

    2024-05-16 00:22:16       27 阅读
  2. 一个用Kotlin编写简易的串行任务调度器

    2024-05-16 00:22:16       33 阅读
  3. 根据标签最大层面ROI提取原始图像区域(二)

    2024-05-16 00:22:16       27 阅读
  4. AOP面向切面编程

    2024-05-16 00:22:16       35 阅读
  5. [初学rust] 03_所有权

    2024-05-16 00:22:16       35 阅读
  6. 大前端之Flex 布局

    2024-05-16 00:22:16       37 阅读
  7. Linux-笔记 man手册命令

    2024-05-16 00:22:16       38 阅读
  8. linq常用方法

    2024-05-16 00:22:16       36 阅读
  9. FAT32 文件系统详解

    2024-05-16 00:22:16       33 阅读