<!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;
height: 100vh;
overflow: hidden;
background: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
cursor: none;
}
.avatar-container {
position: relative;
width: 300px;
height: 300px;
}
.avatar {
position: absolute;
width: 100px;
height: 100px;
border-radius: 50%;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
transition: transform 0.1s ease-out;
transform-origin: center center;
}
.avatar:nth-child(1) {
z-index: 5;
filter: brightness(1.1);
}
.avatar:nth-child(2) {
z-index: 4;
filter: brightness(0.9) blur(1px);
transition-delay: 0.05s;
}
.avatar:nth-child(3) {
z-index: 3;
filter: brightness(0.8) blur(2px);
transition-delay: 0.1s;
}
.avatar:nth-child(4) {
z-index: 2;
filter: brightness(0.7) blur(3px);
transition-delay: 0.15s;
}
.avatar:nth-child(5) {
z-index: 1;
filter: brightness(0.6) blur(4px);
transition-delay: 0.2s;
}
.info {
position: absolute;
bottom: -50px;
left: 0;
width: 100%;
text-align: center;
color: #666;
font-size: 14px;
}
</style>
</head>
<body>
<div class="avatar-container">
<div class="avatar"></div>
<div class="avatar"></div>
<div class="avatar"></div>
<div class="avatar"></div>
<div class="avatar"></div>
<div class="info">鼠标移动查看跟随效果</div>
</div>
<script>
document.addEventListener('mousemove', function(e) {
const avatars = document.querySelectorAll('.avatar');
const container = document.querySelector('.avatar-container');
const rect = container.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const mouseX = e.clientX;
const mouseY = e.clientY;
const angle = Math.atan2(mouseY - centerY, mouseX - centerX);
const distance = Math.min(
Math.sqrt(Math.pow(mouseX - centerX, 2) + Math.pow(mouseY - centerY, 2)) * 0.3,
50
);
avatars.forEach((avatar, index) => {
const offset = (index + 1) * 5;
const x = Math.cos(angle) * (distance + offset);
const y = Math.sin(angle) * (distance + offset);
const scale = 1 - index * 0.1;
avatar.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;
});
});
</script>
</body>
</html>