Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

写一个纯前端的抽奖程序

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro’s answer.
  1. User

    写一个纯前端的抽奖程序

  2. Qwen Assistant

    以下是一个基于 HTML/CSS/JavaScript 实现的纯前端抽奖程序,具备动画效果和交互逻辑:


    🎉 完整代码(可直接复制为 .html 文件运行)

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>纯前端抽奖程序</title>
        <style>
            /* 基础样式 */
            body {
                font-family: 'Segoe UI', sans-serif;
                display: flex;
                justify-content: center;
                align-items: center;
                min-height: 100vh;
                margin: 0;
                background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
                color: #333;
            }
    
            .container {
                background: white;
                padding: 2rem;
                border-radius: 12px;
                box-shadow: 0 8px 24px rgba(0,0,0,0.1);
                width: 400px;
                max-width: 90%;
            }
    
            h2 {
                text-align: center;
                margin-bottom: 1.5rem;
            }
    
            textarea {
                width: 100%;
                height: 150px;
                padding: 0.8rem;
                border-radius: 8px;
                border: 1px solid #ccc;
                resize: none;
                font-size: 1rem;
                margin-bottom: 1rem;
                box-sizing: border-box;
            }
    
            .controls {
                display: flex;
                gap: 1rem;
                margin-bottom: 1.5rem;
            }
    
            input[type="number"] {
                flex: 1;
                padding: 0.6rem;
                border-radius: 6px;
                border: 1px solid #ccc;
                font-size: 1rem;
                text-align: center;
            }
    
            button {
                padding: 0.6rem 1.2rem;
                background: #3f51b5;
                color: white;
                border: none;
                border-radius: 6px;
                font-size: 1rem;
                cursor: pointer;
                transition: background 0.3s;
            }
    
            button:hover {
                background: #303f9f;
            }
    
            #result {
                min-height: 60px;
                font-size: 1.2rem;
                font-weight: bold;
                color: #2e7d32;
                text-align: center;
                margin-bottom: 1rem;
                transition: all 0.3s ease;
            }
    
            .winner {
                animation: pop 0.5s ease-in-out;
            }
    
            @keyframes pop {
                0% { transform: scale(1); }
                50% { transform: scale(1.2); }
                100% { transform: scale(1); }
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h2>🎉 抽奖程序</h2>
            <textarea id="participants" placeholder="请输入姓名(每行一个)...&#10张三&#10李四&#10王五"></textarea>
            <div class="controls">
                <input type="number" id="winnersCount" min="1" value="1" placeholder="奖项数量">
                <button onclick="startLottery()">开始抽奖</button>
            </div>
            <div id="result"></div>
        </div>
    
        <script>
            // 抽奖逻辑
            function startLottery() {
                const textarea = document.getElementById('participants');
                const winnersCountInput = document.getElementById('winnersCount');
                const resultDiv = document.getElementById('result');
                
                // 解析输入
                const names = textarea.value.split('\n')
                    .map(name => name.trim())
                    .filter(name => name.length > 0);
    
                let winnersCount = parseInt(winnersCountInput.value) || 1;
    
                // 错误处理
                if (names.length === 0) {
                    alert('请先输入至少一个参与者姓名');
                    return;
                }
    
                if (winnersCount < 1) {
                    alert('奖项数量必须大于等于1');
                    return;
                }
    
                if (winnersCount > names.length) {
                    alert(`奖项数量不能超过参与者数量(${names.length})`);
                    return;
                }
    
                // 动画配置
                let currentRound = 0;
                const totalRounds = 20;
                const interval = 100;
                const originalNames = [...names];
    
                // 开始动画
                const timer = setInterval(() => {
                    if (currentRound >= totalRounds) {
                        clearInterval(timer);
                        // 选择最终结果
                        const winners = [];
                        while (winners.length < winnersCount) {
                            const randomIndex = Math.floor(Math.random() * names.length);
                            const winner = names.splice(randomIndex, 1)[0];
                            winners.push(winner);
                        }
                        resultDiv.className = 'winner';
                        resultDiv.innerHTML = `🏆 中奖者:${winners.join('<br>')}<br><small>(共 ${winnersCount} 个奖项)</small>`;
                    } else {
                        // 滚动动画
                        const randomName = originalNames[Math.floor(Math.random() * originalNames.length)];
                        resultDiv.className = '';
                        resultDiv.textContent = randomName;
                        currentRound++;
                    }
                }, interval);
            }
        </script>
    </body>
    </html>
    

    🧩 核心功能说明

    功能模块实现细节
    输入解析支持换行分隔的姓名输入(自动去除空白行)
    奖项控制可设置 1~N 个奖项(自动校验合法性)
    抽奖算法Fisher-Yates 洗牌算法变体(保证唯一性)
    动画效果快速滚动名字 → 渐停 → 弹跳显示结果
    错误处理检查空输入、非法奖项数量等
    响应式设计自适应移动设备屏幕

    🔍 使用说明

    1. 输入名单:在文本框中按行输入姓名(如:张三↵李四↵王五)
    2. 设置奖项:修改右侧数字选择框(默认 1 个奖项)
    3. 开始抽奖:点击按钮后会看到名字快速滚动
    4. 查看结果:最终中奖者会以弹跳动画显示

    🛠️ 可扩展方向

    1. 持久化存储:添加 localStorage 记录历史中奖名单
    2. 声音效果:在抽奖过程中加入背景音乐或提示音
    3. 多轮抽奖:支持不同奖项分别抽取(如一等奖、二等奖)
    4. 导出功能:将结果导出为 CSV 或 Excel 文件
    5. 暗色模式:根据系统偏好自动切换主题

    这个程序完全运行在浏览器端,无需任何服务器支持,适合快速举办小型抽奖活动。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.