SVG长按左键挥手动画特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SVG长按左键挥手动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
            overflow: hidden;
        }
        
        .container {
            position: relative;
            width: 300px;
            height: 300px;
        }
        
        #hand {
            width: 100%;
            height: 100%;
            cursor: pointer;
        }
        
        .instructions {
            margin-top: 30px;
            text-align: center;
            color: #333;
        }
        
        .link {
            position: absolute;
            bottom: 20px;
            color: #666;
            text-align: center;
            width: 100%;
        }
        
        .link a {
            color: #ff6600;
            text-decoration: none;
        }
        
        .link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <svg id="hand" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
            <!-- 手臂 -->
            <path id="arm" fill="#ffcc99" d="M100,50 L100,150 C100,170 120,180 140,170 L150,160 C160,150 160,140 150,130 L100,100" />
            
            <!-- 手掌 -->
            <circle id="palm" cx="100" cy="100" r="30" fill="#ffcc99" />
            
            <!-- 手指 -->
            <path id="finger1" fill="#ffcc99" d="M70,90 L80,70 C90,60 100,60 110,70 L120,90" />
            <path id="finger2" fill="#ffcc99" d="M80,80 L90,60 C100,50 110,50 120,60 L130,80" />
            <path id="finger3" fill="#ffcc99" d="M90,70 L100,50 C110,40 120,40 130,50 L140,70" />
            <path id="finger4" fill="#ffcc99" d="M100,60 L110,40 C120,30 130,30 140,40 L150,60" />
            
            <!-- 初始状态隐藏的挥手动画路径 -->
            <path id="wavePath" fill="none" stroke="none" d="M100,100 Q80,80 100,60 Q120,40 140,60 Q160,80 140,100" />
        </svg>
    </div>
    
    <div class="instructions">
        长按鼠标左键查看挥手动画
    </div>
    

    <script>
        const hand = document.getElementById('hand');
        let isMouseDown = false;
        let animationInterval;
        
        // 鼠标按下事件
        hand.addEventListener('mousedown', (e) => {
            if (e.button === 0) { // 左键
                isMouseDown = true;
                startWaveAnimation();
            }
        });
        
        // 鼠标释放事件
        document.addEventListener('mouseup', () => {
            if (isMouseDown) {
                isMouseDown = false;
                stopWaveAnimation();
            }
        });
        
        // 开始挥手动画
        function startWaveAnimation() {
            let waveCount = 0;
            const maxWaves = 5;
            
            animationInterval = setInterval(() => {
                if (waveCount >= maxWaves * 2) {
                    waveCount = 0;
                }
                
                const waveOffset = waveCount % 2 === 0 ? 10 : -10;
                animateHand(waveOffset);
                waveCount++;
            }, 200);
        }
        
        // 停止挥手动画
        function stopWaveAnimation() {
            clearInterval(animationInterval);
            resetHandPosition();
        }
        
        // 动画手部位置
        function animateHand(offset) {
            const palm = document.getElementById('palm');
            const arm = document.getElementById('arm');
            const fingers = [
                document.getElementById('finger1'),
                document.getElementById('finger2'),
                document.getElementById('finger3'),
                document.getElementById('finger4')
            ];
            
            // 移动手掌
            palm.setAttribute('cx', 100 + offset);
            
            // 移动手臂
            arm.setAttribute('d', `M100,50 L100,150 C100,170 120,180 140,170 L150,160 C160,150 160,140 ${150 + offset},130 L${100 + offset},100`);
            
            // 移动手指
            fingers.forEach((finger, index) => {
                const yOffset = 30 - (index * 10);
                finger.setAttribute('d', 
                    `M${70 + (index * 10) + offset},${90 - (index * 10)} 
                     L${80 + (index * 10) + offset},${70 - (index * 10)} 
                     C${90 + (index * 10) + offset},${60 - (index * 10)} 
                      ${100 + (index * 10) + offset},${50 - (index * 10)} 
                      ${110 + (index * 10) + offset},${60 - (index * 10)} 
                     L${120 + (index * 10) + offset},${80 - (index * 10)}`);
            });
        }
        
        // 重置手部位置
        function resetHandPosition() {
            const palm = document.getElementById('palm');
            const arm = document.getElementById('arm');
            const fingers = [
                document.getElementById('finger1'),
                document.getElementById('finger2'),
                document.getElementById('finger3'),
                document.getElementById('finger4')
            ];
            
            // 重置手掌
            palm.setAttribute('cx', 100);
            
            // 重置手臂
            arm.setAttribute('d', 'M100,50 L100,150 C100,170 120,180 140,170 L150,160 C160,150 160,140 150,130 L100,100');
            
            // 重置手指
            fingers[0].setAttribute('d', 'M70,90 L80,70 C90,60 100,60 110,70 L120,90');
            fingers[1].setAttribute('d', 'M80,80 L90,60 C100,50 110,50 120,60 L130,80');
            fingers[2].setAttribute('d', 'M90,70 L100,50 C110,40 120,40 130,50 L140,70');
            fingers[3].setAttribute('d', 'M100,60 L110,40 C120,30 130,30 140,40 L150,60');
        }
    </script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值