眼睛随鼠标运动
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>眼睛跟随鼠标</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: #f0f0f0;
overflow: hidden;
}
.face {
position: relative;
width: 200px;
height: 200px;
background: #ffdbac;
border-radius: 50%;
box-shadow: 0 0 20px rgba(0,0,0,0.1);
margin-bottom: 30px;
}
.eye {
position: absolute;
width: 60px;
height: 60px;
background: white;
border-radius: 50%;
top: 60px;
}
.eye.left {
left: 30px;
}
.eye.right {
right: 30px;
}
.pupil {
position: absolute;
width: 20px;
height: 20px;
background: black;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.nose {
position: absolute;
width: 20px;
height: 10px;
background: #d9a777;
border-radius: 50%;
top: 110px;
left: 50%;
transform: translateX(-50%);
}
.cheek {
position: absolute;
width: 38px;
height: 22px;
background: rgba(255, 105, 135, 0.45);
border-radius: 50%;
top: 128px;
opacity: 0;
transform: scale(0.9);
transition: opacity 0.2s ease, transform 0.2s ease;
filter: blur(0.2px);
}
.cheek.left { left: 28px; }
.cheek.right { right: 28px; }
.mouth {
position: absolute;
width: 60px;
height: 30px;
border-radius: 0 0 30px 30px;
border: 3px solid #d94f4f;
border-top: none;
top: 130px;
left: 50%;
transform: translateX(-50%);
transition: all 0.3s ease;
}
.mouth.happy {
border-radius: 0 0 30px 30px;
border-top: none;
top: 132px;
height: 30px;
}
.mouth.sad {
border-radius: 30px 30px 0 0;
border-top: 3px solid #d94f4f;
border-bottom: none;
top: 140px;
height: 24px;
}
.face.happy .cheek {
opacity: 1;
transform: scale(1);
}
.buttons {
display: flex;
gap: 20px;
}
.btn {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border: none;
border-radius: 5px;
background: #4CAF50;
color: white;
}
.btn.reject {
background: #f44336;
}
</style>
</head>
<body>
<div class="face">
<div class="eye left"><div class="pupil" id="pupil-left"></div></div>
<div class="eye right"><div class="pupil" id="pupil-right"></div></div>
<div class="nose"></div>
<div class="cheek left"></div>
<div class="cheek right"></div>
<div class="mouth" id="mouth"></div>
</div>
<div class="buttons">
<button class="btn" id="approve">通过</button>
<button class="btn reject" id="reject">拒绝</button>
</div>
<script>
const pupils = document.querySelectorAll('.pupil');
const face = document.querySelector('.face');
const mouth = document.getElementById('mouth');
const approveBtn = document.getElementById('approve');
const rejectBtn = document.getElementById('reject');
document.addEventListener('mousemove', (e) => {
const faceRect = face.getBoundingClientRect();
const faceCenterX = faceRect.left + faceRect.width / 2;
const faceCenterY = faceRect.top + faceRect.height / 2;
const deltaX = e.clientX - faceCenterX;
const deltaY = e.clientY - faceCenterY;
// 最大偏移距离(限制眼球移动范围)
const maxOffset = 15;
// 计算角度
const angle = Math.atan2(deltaY, deltaX);
// 计算 pupil 新位置
const offsetX = Math.cos(angle) * maxOffset;
const offsetY = Math.sin(angle) * maxOffset;
pupils.forEach(pupil => {
pupil.style.transform = `translate(calc(-50% + ${offsetX}px), calc(-50% + ${offsetY}px))`;
});
});
// 按钮事件
approveBtn.addEventListener('mouseenter', () => {
face.classList.add('happy');
mouth.className = 'mouth happy';
});
approveBtn.addEventListener('mouseleave', () => {
face.classList.remove('happy');
mouth.className = 'mouth';
});
rejectBtn.addEventListener('mouseenter', () => {
face.classList.remove('happy');
mouth.className = 'mouth sad';
});
rejectBtn.addEventListener('mouseleave', () => {
mouth.className = 'mouth';
});
</script>
</body>
</html>
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 从安的博客!
评论



