卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章1829本站已运行4109

deepseek自动生成的html九宫格抽奖页面

<!DOCTYPE html>
<html>
<head>
    <style>
        .grid {
            display: grid;
            grid-template-columns: repeat(3, 100px);
            grid-template-rows: repeat(3, 100px);
            gap: 5px;
            margin: 20px;
        }
 
        .cell {
            background: white;
            border: 2px solid #ccc;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            cursor: pointer;
        }
 
        #start {
            background: red;
            color: white;
            border: none;
            font-size: 20px;
            cursor: pointer;
        }
 
        .highlight {
            background: yellow !important;
        }
    </style>
</head>
<body>
    <div class="grid">
        <div class="cell" data-id="1">一等奖</div>
        <div class="cell" data-id="2">参与奖</div>
        <div class="cell" data-id="3">三等奖</div>
        <div class="cell" data-id="4">谢谢参与</div>
        <button id="start">开始</button>
        <div class="cell" data-id="6">一等奖</div>
        <div class="cell" data-id="7">参与奖</div>
        <div class="cell" data-id="8">二等奖</div>
        <div class="cell" data-id="9">一等奖</div>
    </div>
 
    <script>
        const prizes = {
            '一等奖': { probability: 3, grids: [1, 6, 9] },
            '二等奖': { probability: 2, grids: [8] },
            '三等奖': { probability: 1, grids: [3] },
            '参与奖': { probability: 20, grids: [2, 7] },
            '谢谢参与': { probability: 74, grids: [4] }
        };
 
        const order = [1, 2, 3, 6, 9, 8, 7, 4]; // 顺时针移动顺序
        let isRunning = false;
        let currentIndex = 0;
        let timer = null;
        const baseSpeed = 100;
 
        document.getElementById('start').addEventListener('click', startLottery);
 
        function getRandomPrize() {
            let random = Math.random() * 100;
            let cumulative = 0;
            for (const [name, config] of Object.entries(prizes)) {
                cumulative += config.probability;
                if (random <= cumulative) {
                    const randomGrid = config.grids[Math.floor(Math.random() * config.grids.length)];
                    return { name, grid: randomGrid };
                }
            }
        }
 
        function updateHighlight() {
            document.querySelectorAll('.cell').forEach(cell => {
                cell.classList.remove('highlight');
            });
            const currentGrid = order[currentIndex];
            document.querySelector(`.cell[data-id="${currentGrid}"]`).classList.add('highlight');
        }
 
        function startLottery() {
            if (isRunning) {
                // 停止抽奖
                isRunning = false;
                clearInterval(timer);
                 
                const result = getRandomPrize();
                const targetIndex = order.indexOf(result.grid);
                 
                // 计算需要移动的步数(转2圈+到达目标)
                const laps = 2;
                const stepsToTarget = (targetIndex - currentIndex + order.length) % order.length;
                const totalSteps = laps * order.length + stepsToTarget;
                 
                let stepsMoved = 0;
                timer = setInterval(() => {
                    currentIndex = (currentIndex + 1) % order.length;
                    updateHighlight();
                    if (++stepsMoved >= totalSteps) {
                        clearInterval(timer);
                        setTimeout(() => alert(`恭喜获得:${result.name}`), 100);
                    }
                }, baseSpeed);
            } else {
                // 开始抽奖
                isRunning = true;
                timer = setInterval(() => {
                    currentIndex = (currentIndex + 1) % order.length;
                    updateHighlight();
                }, baseSpeed);
            }
        }
    </script>
</body>
</html>
 
卓越飞翔博客
上一篇: 孟菲斯风格天气HTML卡片源码
下一篇: deepseek生成的“拯救选择困难症”小工具
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏