CSS3点击按钮爆炸特效

<!DOCTYPE html>
<html lang="zh-CN">
<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;
            min-height: 100vh;
            background: #f0f0f0;
            font-family: Arial, sans-serif;
        }
        
        .container {
            text-align: center;
        }
        
        /* 按钮样式 */
        .explode-btn {
            position: relative;
            padding: 15px 30px;
            font-size: 18px;
            color: white;
            background: linear-gradient(45deg, #ff5252, #ff4081);
            border: none;
            border-radius: 50px;
            cursor: pointer;
            box-shadow: 0 5px 15px rgba(255, 82, 82, 0.4);
            overflow: hidden;
            outline: none;
            transition: transform 0.3s, box-shadow 0.3s;
            z-index: 1;
        }
        
        .explode-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(255, 82, 82, 0.6);
        }
        
        .explode-btn:active {
            transform: translateY(1px);
        }
        
        /* 爆炸粒子 */
        .particles {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            pointer-events: none;
        }
        
        .particle {
            position: absolute;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background: #ffeb3b;
            opacity: 0;
        }
        
        /* 爆炸动画 */
        .explode-btn:focus {
            animation: explode 0.5s forwards;
        }
        
        .explode-btn:focus .particle {
            animation: particle-move 1s forwards;
        }
        
        @keyframes explode {
            0% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(1.2);
                opacity: 0.8;
            }
            100% {
                transform: scale(0);
                opacity: 0;
            }
        }
        
        @keyframes particle-move {
            0% {
                transform: translate(0, 0);
                opacity: 1;
            }
            100% {
                transform: translate(var(--tx), var(--ty));
                opacity: 0;
            }
        }
        
        /* 重新出现动画 */
        .reappear {
            animation: reappear 0.5s 1s forwards;
            opacity: 0;
            transform: scale(0);
        }
        
        @keyframes reappear {
            0% {
                transform: scale(0);
                opacity: 0;
            }
            70% {
                transform: scale(1.1);
                opacity: 1;
            }
            100% {
                transform: scale(1);
                opacity: 1;
            }
        }
        
        /* 底部链接 */
        .footer-link {
            position: fixed;
            bottom: 20px;
            color: #666;
            text-decoration: none;
            font-size: 14px;
            transition: color 0.3s;
        }
        
        .footer-link:hover {
            color: #ff5252;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <button class="explode-btn" id="explodeBtn">
            点击爆炸
            <div class="particles" id="particles"></div>
        </button>
    </div>
    

    <script>
        const explodeBtn = document.getElementById('explodeBtn');
        const particlesContainer = document.getElementById('particles');
        
        // 创建爆炸粒子
        function createParticles() {
            // 清空现有粒子
            particlesContainer.innerHTML = '';
            
            // 创建30个粒子
            for (let i = 0; i < 30; i++) {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                
                // 随机颜色
                const colors = ['#ffeb3b', '#ff5252', '#ff4081', '#e91e63', '#9c27b0'];
                const randomColor = colors[Math.floor(Math.random() * colors.length)];
                particle.style.background = randomColor;
                
                // 随机大小
                const size = Math.random() * 8 + 4;
                particle.style.width = `${size}px`;
                particle.style.height = `${size}px`;
                
                // 随机位置和运动方向
                const angle = Math.random() * Math.PI * 2;
                const distance = Math.random() * 100 + 50;
                const tx = Math.cos(angle) * distance;
                const ty = Math.sin(angle) * distance;
                
                particle.style.setProperty('--tx', `${tx}px`);
                particle.style.setProperty('--ty', `${ty}px`);
                
                // 随机动画延迟
                particle.style.animationDelay = `${Math.random() * 0.5}s`;
                
                particlesContainer.appendChild(particle);
            }
        }
        
        // 点击按钮事件
        explodeBtn.addEventListener('click', function() {
            // 创建粒子
            createParticles();
            
            // 触发爆炸动画
            this.classList.add('reappear');
            this.focus();
            
            // 重置按钮状态
            setTimeout(() => {
                this.classList.remove('reappear');
            }, 1500);
        });
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值