CSS3转盘抽奖动画特效

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS3转盘抽奖动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: #f5f5f5;
            font-family: 'Microsoft YaHei', sans-serif;
            overflow: hidden;
        }

        .lottery-container {
            position: relative;
            width: 400px;
            height: 400px;
        }

        .wheel {
            position: relative;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background: #fff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            transition: transform 4s cubic-bezier(0.17, 0.67, 0.12, 0.99);
            transform: rotate(0deg);
        }

        .wheel.spinning {
            transform: rotate(1800deg); /* 5圈 */
        }

        .wheel-center {
            position: absolute;
            width: 60px;
            height: 60px;
            background: #e74c3c;
            border-radius: 50%;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 10;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
            cursor: pointer;
        }

        .wheel-center::before {
            content: '';
            position: absolute;
            width: 0;
            height: 0;
            border-left: 15px solid transparent;
            border-right: 15px solid transparent;
            border-bottom: 30px solid #e74c3c;
            top: -25px;
            left: 50%;
            transform: translateX(-50%);
        }

        .section {
            position: absolute;
            width: 50%;
            height: 50%;
            transform-origin: 100% 100%;
            left: 0;
            top: 0;
            border: 1px solid #ddd;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: center;
            padding-left: 50px;
            font-size: 16px;
            font-weight: bold;
            color: white;
            text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }

        /* 8个扇区 */
        .section:nth-child(1) {
            background: #e74c3c;
            transform: rotate(0deg) skewY(45deg);
        }
        .section:nth-child(2) {
            background: #3498db;
            transform: rotate(45deg) skewY(45deg);
        }
        .section:nth-child(3) {
            background: #2ecc71;
            transform: rotate(90deg) skewY(45deg);
        }
        .section:nth-child(4) {
            background: #f1c40f;
            transform: rotate(135deg) skewY(45deg);
        }
        .section:nth-child(5) {
            background: #9b59b6;
            transform: rotate(180deg) skewY(45deg);
        }
        .section:nth-child(6) {
            background: #1abc9c;
            transform: rotate(225deg) skewY(45deg);
        }
        .section:nth-child(7) {
            background: #d35400;
            transform: rotate(270deg) skewY(45deg);
        }
        .section:nth-child(8) {
            background: #34495e;
            transform: rotate(315deg) skewY(45deg);
        }

        .section-text {
            transform: skewY(-45deg) rotate(22.5deg);
            text-align: center;
            width: 80px;
        }

        .pointer {
            position: absolute;
            width: 0;
            height: 0;
            border-left: 20px solid transparent;
            border-right: 20px solid transparent;
            border-top: 40px solid #e74c3c;
            top: -20px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 5;
            filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.3));
        }

        .result {
            position: absolute;
            bottom: -60px;
            left: 50%;
            transform: translateX(-50%);
            background: #fff;
            padding: 10px 20px;
            border-radius: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            font-size: 18px;
            font-weight: bold;
            color: #e74c3c;
            opacity: 0;
            transition: opacity 0.5s ease;
        }

        .result.show {
            opacity: 1;
        }

        .confetti {
            position: absolute;
            width: 10px;
            height: 10px;
            background: #f00;
            opacity: 0;
            z-index: 20;
        }

        .hidden-link {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            color: rgba(0, 0, 0, 0.3);
            font-size: 12px;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="lottery-container">
        <div class="pointer"></div>
        <div class="wheel" id="wheel">
            <div class="section"><div class="section-text">一等奖</div></div>
            <div class="section"><div class="section-text">二等奖</div></div>
            <div class="section"><div class="section-text">三等奖</div></div>
            <div class="section"><div class="section-text">四等奖</div></div>
            <div class="section"><div class="section-text">五等奖</div></div>
            <div class="section"><div class="section-text">六等奖</div></div>
            <div class="section"><div class="section-text">七等奖</div></div>
            <div class="section"><div class="section-text">八等奖</div></div>
        </div>
        <div class="wheel-center" id="startBtn">开始抽奖</div>
        <div class="result" id="result"></div>
    </div>

    <div id="confetti-container"></div>

    <script>
        const wheel = document.getElementById('wheel');
        const startBtn = document.getElementById('startBtn');
        const result = document.getElementById('result');
        const confettiContainer = document.getElementById('confetti-container');
        const prizes = ['一等奖', '二等奖', '三等奖', '四等奖', '五等奖', '六等奖', '七等奖', '八等奖'];
        let isSpinning = false;

        // 开始抽奖
        startBtn.addEventListener('click', function() {
            if (isSpinning) return;
            isSpinning = true;
            
            // 重置转盘
            wheel.style.transform = 'rotate(0deg)';
            wheel.classList.remove('spinning');
            result.classList.remove('show');
            result.textContent = '';
            confettiContainer.innerHTML = '';
            
            // 随机选择奖项 (这里设置三等奖概率高一些作为演示)
            let randomPrize;
            const rand = Math.random();
            if (rand < 0.1) {
                randomPrize = 0; // 一等奖 10%
            } else if (rand < 0.3) {
                randomPrize = 1; // 二等奖 20%
            } else if (rand < 0.6) {
                randomPrize = 2; // 三等奖 30%
            } else if (rand < 0.7) {
                randomPrize = 3; // 四等奖 10%
            } else if (rand < 0.8) {
                randomPrize = 4; // 五等奖 10%
            } else if (rand < 0.85) {
                randomPrize = 5; // 六等奖 5%
            } else if (rand < 0.95) {
                randomPrize = 6; // 七等奖 10%
            } else {
                randomPrize = 7; // 八等奖 5%
            }
            
            // 计算停止角度 (每个扇区45度)
            const stopAngle = 360 - (randomPrize * 45 + 22.5); // 22.5是半个扇区的角度
            
            // 开始旋转
            setTimeout(() => {
                wheel.classList.add('spinning');
                wheel.style.transform = `rotate(${stopAngle}deg)`;
            }, 10);
            
            // 旋转结束后显示结果
            setTimeout(() => {
                isSpinning = false;
                result.textContent = `恭喜获得: ${prizes[randomPrize]}`;
                result.classList.add('show');
                
                // 如果中了一等奖,放烟花
                if (randomPrize === 0) {
                    createConfetti();
                }
            }, 4100);
        });
        
        // 创建彩色纸屑效果
        function createConfetti() {
            const colors = ['#e74c3c', '#3498db', '#2ecc71', '#f1c40f', '#9b59b6', '#1abc9c', '#d35400', '#34495e'];
            
            for (let i = 0; i < 100; i++) {
                setTimeout(() => {
                    const confetti = document.createElement('div');
                    confetti.classList.add('confetti');
                    confetti.style.left = `${Math.random() * 100}%`;
                    confetti.style.top = `${Math.random() * 100}%`;
                    confetti.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    confetti.style.transform = `rotate(${Math.random() * 360}deg)`;
                    confetti.style.width = `${5 + Math.random() * 10}px`;
                    confetti.style.height = `${5 + Math.random() * 10}px`;
                    confetti.style.borderRadius = Math.random() > 0.5 ? '50%' : '0';
                    
                    confettiContainer.appendChild(confetti);
                    
                    // 动画
                    setTimeout(() => {
                        confetti.style.opacity = '1';
                        confetti.style.transform = `translate(${(Math.random() - 0.5) * 200}px, ${Math.random() * 200 + 100}px) rotate(${Math.random() * 360}deg)`;
                        confetti.style.transition = `all ${1 + Math.random() * 2}s ease-out`;
                        
                        // 移除
                        setTimeout(() => {
                            confetti.remove();
                        }, 3000);
                    }, 10);
                }, i * 30);
            }
        }
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值