HTML5 SVG糖果爱心动画特效

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;
        }

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

        .candy-heart {
            position: absolute;
            width: 200px;
            height: 180px;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            filter: drop-shadow(0 0 15px rgba(233, 30, 99, 0.5));
            animation: pulse 2s ease infinite;
        }

        .candy-heart path {
            stroke: #c2185b;
            stroke-width: 5;
            fill: #e91e63;
            stroke-dasharray: 1550;
            stroke-dashoffset: 1550;
            animation: draw 3s ease-in-out forwards;
        }

        @keyframes draw {
            to {
                stroke-dashoffset: 0;
            }
        }

        @keyframes pulse {
            0%, 100% {
                transform: translate(-50%, -50%) scale(1);
            }
            50% {
                transform: translate(-50%, -50%) scale(1.1);
            }
        }

        .candy-stripes {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            overflow: visible;
        }

        .stripe {
            position: absolute;
            width: 200px;
            height: 20px;
            background: rgba(255, 255, 255, 0.3);
            transform-origin: left center;
            animation: stripe-animation 4s ease-in-out infinite;
        }

        @keyframes stripe-animation {
            0%, 100% {
                transform: rotate(0deg) translateY(0);
                opacity: 0.3;
            }
            50% {
                transform: rotate(5deg) translateY(-10px);
                opacity: 0.7;
            }
        }

        .candy-dots {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }

        .dot {
            position: absolute;
            width: 15px;
            height: 15px;
            background: white;
            border-radius: 50%;
            opacity: 0.8;
            animation: dot-float 5s ease-in-out infinite;
        }

        @keyframes dot-float {
            0%, 100% {
                transform: translate(0, 0);
            }
            25% {
                transform: translate(5px, -5px);
            }
            50% {
                transform: translate(0, -10px);
            }
            75% {
                transform: translate(-5px, -5px);
            }
        }

        .candy-highlights {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }

        .highlight {
            position: absolute;
            width: 40px;
            height: 20px;
            background: rgba(255, 255, 255, 0.4);
            border-radius: 50%;
            filter: blur(5px);
            animation: highlight-shine 3s ease-in-out infinite;
        }

        @keyframes highlight-shine {
            0%, 100% {
                opacity: 0.3;
            }
            50% {
                opacity: 0.7;
            }
        }

        .message {
            position: absolute;
            bottom: 50px;
            left: 50%;
            transform: translateX(-50%);
            font-size: 24px;
            color: #e91e63;
            text-align: center;
            text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
            opacity: 0;
            animation: fade-in 2s ease 1s forwards;
        }

        @keyframes fade-in {
            to {
                opacity: 1;
            }
        }

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

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

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

        .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="candy-heart-container">
        <svg class="candy-heart" viewBox="0 0 200 180">
            <path d="M100,30 C120,0 180,20 180,70 C180,120 100,170 100,170 C100,170 20,120 20,70 C20,20 80,0 100,30 Z"/>
        </svg>
        
        <div class="candy-stripes">
            <div class="stripe" style="top: 30%; left: 10%; animation-delay: 0s;"></div>
            <div class="stripe" style="top: 50%; left: 10%; animation-delay: 0.5s;"></div>
            <div class="stripe" style="top: 70%; left: 10%; animation-delay: 1s;"></div>
        </div>
        
        <div class="candy-dots">
            <div class="dot" style="top: 40%; left: 30%; animation-delay: 0s;"></div>
            <div class="dot" style="top: 60%; left: 25%; animation-delay: 0.5s;"></div>
            <div class="dot" style="top: 50%; left: 70%; animation-delay: 1s;"></div>
            <div class="dot" style="top: 30%; left: 65%; animation-delay: 1.5s;"></div>
            <div class="dot" style="top: 70%; left: 60%; animation-delay: 2s;"></div>
        </div>
        
        <div class="candy-highlights">
            <div class="highlight" style="top: 20%; left: 30%; animation-delay: 0s;"></div>
            <div class="highlight" style="top: 70%; left: 60%; animation-delay: 1s;"></div>
        </div>
        
        <div class="floating-candies" id="floating-candies"></div>
        
        <div class="message">甜蜜情人节<br>爱如糖果般甜美</div>
    </div>

    <script>
        const floatingCandies = document.getElementById('floating-candies');
        const candyColors = ['#e91e63', '#ff4081', '#f50057', '#ff80ab', '#ffeb3b', '#4caf50'];
        
        // 创建漂浮糖果
        function createFloatingCandies() {
            for (let i = 0; i < 8; i++) {
                setTimeout(() => {
                    const candy = document.createElement('div');
                    candy.classList.add('floating-candy');
                    
                    // 随机形状(心形或圆形)
                    const isHeart = Math.random() > 0.5;
                    const size = Math.random() * 30 + 20;
                    
                    if (isHeart) {
                        candy.innerHTML = `
                            <svg width="${size}" height="${size}" viewBox="0 0 20 18">
                                <path fill="${candyColors[Math.floor(Math.random() * candyColors.length)]}" 
                                      d="M10,3 C12,0 16,1 16,5 C16,9 10,13 10,13 C10,13 4,9 4,5 C4,1 8,0 10,3 Z"/>
                            </svg>
                        `;
                    } else {
                        candy.innerHTML = `
                            <svg width="${size}" height="${size}" viewBox="0 0 20 20">
                                <circle cx="10" cy="10" r="8" 
                                        fill="${candyColors[Math.floor(Math.random() * candyColors.length)]}"/>
                            </svg>
                        `;
                    }
                    
                    // 随机位置
                    candy.style.left = `${Math.random() * 100}%`;
                    candy.style.bottom = '0';
                    
                    // 随机动画持续时间
                    const duration = Math.random() * 4 + 3;
                    candy.style.animationDuration = `${duration}s`;
                    
                    // 随机旋转
                    candy.style.transform = `rotate(${Math.random() * 360}deg)`;
                    
                    floatingCandies.appendChild(candy);
                    
                    // 动画结束后移除
                    setTimeout(() => {
                        candy.remove();
                    }, duration * 1000);
                }, i * 300);
            }
        }
        
        // 初始创建漂浮糖果
        createFloatingCandies();
        
        // 每隔一段时间创建新的漂浮糖果
        setInterval(createFloatingCandies, 2000);
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值