<!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>
3135

被折叠的 条评论
为什么被折叠?



