HTML5 SVG情人节盒子动画特效

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML5 SVG情人节盒子动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background: #ffebee;
            overflow: hidden;
            font-family: 'Arial', sans-serif;
        }

        .valentine-box {
            position: relative;
            width: 300px;
            height: 300px;
            cursor: pointer;
        }

        .box {
            position: absolute;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            transition: transform 1s ease-in-out;
            transform: translateZ(-150px) rotateY(0deg);
        }

        .box.open {
            transform: translateZ(-150px) rotateY(-90deg);
        }

        .box-face {
            position: absolute;
            width: 100%;
            height: 100%;
            background: #e91e63;
            border: 5px solid #c2185b;
            box-sizing: border-box;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 24px;
            color: white;
            backface-visibility: hidden;
        }

        .front {
            transform: translateZ(150px);
            background: #ff4081;
        }

        .back {
            transform: rotateY(180deg) translateZ(150px);
            background: #e91e63;
        }

        .right {
            transform: rotateY(90deg) translateZ(150px);
            background: #f50057;
        }

        .left {
            transform: rotateY(-90deg) translateZ(150px);
            background: #ff80ab;
        }

        .top {
            transform: rotateX(90deg) translateZ(150px);
            background: #e91e63;
            height: 300px;
        }

        .bottom {
            transform: rotateX(-90deg) translateZ(150px);
            background: #f50057;
        }

        .heart {
            position: absolute;
            width: 100px;
            height: 100px;
            background: #ffeb3b;
            transform: rotate(45deg);
            animation: pulse 1.5s ease infinite;
        }

        .heart:before,
        .heart:after {
            content: '';
            position: absolute;
            width: 100px;
            height: 100px;
            background: #ffeb3b;
            border-radius: 50%;
        }

        .heart:before {
            top: -50px;
            left: 0;
        }

        .heart:after {
            top: 0;
            left: -50px;
        }

        @keyframes pulse {
            0%, 100% {
                transform: rotate(45deg) scale(1);
            }
            50% {
                transform: rotate(45deg) scale(1.1);
            }
        }

        .hearts-container {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            pointer-events: none;
        }

        .floating-heart {
            position: absolute;
            opacity: 0;
            animation: float-up 4s linear forwards;
        }

        @keyframes float-up {
            0% {
                transform: translateY(0) rotate(0deg);
                opacity: 1;
            }
            100% {
                transform: translateY(-300px) rotate(360deg);
                opacity: 0;
            }
        }

        .message {
            position: absolute;
            width: 80%;
            text-align: center;
            font-size: 18px;
            color: white;
            opacity: 0;
            transition: opacity 0.5s ease;
            z-index: 10;
        }

        .box.open .message {
            opacity: 1;
        }

        .ribbon {
            position: absolute;
            width: 100%;
            height: 30px;
            background: #ffeb3b;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            z-index: 5;
        }

        .bow {
            position: absolute;
            width: 60px;
            height: 60px;
            background: #ffeb3b;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 6;
        }

        .bow:before,
        .bow:after {
            content: '';
            position: absolute;
            width: 80px;
            height: 30px;
            background: #ffeb3b;
            border-radius: 50% 50% 0 0;
            top: 15px;
        }

        .bow:before {
            left: -70px;
            transform: rotate(-45deg);
        }

        .bow:after {
            right: -70px;
            transform: rotate(45deg);
        }

        .hidden-link {
            position: absolute;
            bottom: 20px;
            left: 50%;
            transform: translateX(-50%);
            color: rgba(0, 0, 0, 0.3);
            font-size: 12px;
            text-decoration: none;
            z-index: 100;
        }
    </style>
</head>
<body>
    <div class="valentine-box">
        <div class="box" id="box">
            <div class="box-face front">
                <div class="heart"></div>
                <div class="ribbon"></div>
                <div class="bow"></div>
                <span>点击打开</span>
            </div>
            <div class="box-face back"></div>
            <div class="box-face right"></div>
            <div class="box-face left"></div>
            <div class="box-face top"></div>
            <div class="box-face bottom"></div>
            <div class="message">情人节快乐!<br>我爱你</div>
        </div>
        <div class="hearts-container" id="hearts-container"></div>
    </div>

    <script>
        const box = document.getElementById('box');
        const heartsContainer = document.getElementById('hearts-container');
        
        // 点击盒子打开/关闭
        box.addEventListener('click', function() {
            this.classList.toggle('open');
            
            // 盒子打开时创建漂浮的心形
            if (this.classList.contains('open')) {
                createFloatingHearts();
            }
        });
        
        // 创建漂浮的心形
        function createFloatingHearts() {
            // 先清除所有现有的心形
            heartsContainer.innerHTML = '';
            
            // 创建多个心形
            for (let i = 0; i < 15; i++) {
                setTimeout(() => {
                    const heart = document.createElement('div');
                    heart.classList.add('floating-heart');
                    
                    // 随机大小
                    const size = Math.random() * 30 + 20;
                    heart.style.width = `${size}px`;
                    heart.style.height = `${size}px`;
                    
                    // 随机位置
                    heart.style.left = `${Math.random() * 100}%`;
                    heart.style.bottom = '0';
                    
                    // 随机颜色
                    const colors = ['#ff4081', '#e91e63', '#f50057', '#ff80ab', '#ffeb3b'];
                    heart.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
                    
                    // 随机动画持续时间
                    const duration = Math.random() * 3 + 2;
                    heart.style.animationDuration = `${duration}s`;
                    
                    // 旋转心形
                    heart.style.transform = `rotate(45deg)`;
                    
                    heartsContainer.appendChild(heart);
                    
                    // 动画结束后移除
                    setTimeout(() => {
                        heart.remove();
                    }, duration * 1000);
                }, i * 200);
            }
        }
        
        // 添加CSS动画关键帧
        const style = document.createElement('style');
        style.textContent = `
            .floating-heart:before, .floating-heart:after {
                content: '';
                position: absolute;
                width: 100%;
                height: 100%;
                background: inherit;
                border-radius: 50%;
            }
            .floating-heart:before {
                top: -50%;
                left: 0;
            }
            .floating-heart:after {
                top: 0;
                left: -50%;
            }
        `;
        document.head.appendChild(style);
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值