<!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 {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: black;
margin: 0;
font-family: Arial, sans-serif;
}
/* 机器人面部基础样式 */
#robot-face {
width: 100%;
height: 100%;
background-color: black;
border-radius: 20px;
position: relative;
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
/* 眼睛通用样式 */
.eye {
width: 12%;
height: 40%;
background-color: white;
border-radius: 150px;
position: absolute;
top: 30%;
transition: all 0.8s ease;
}
/* 左眼位置 */
#left-eye {
left: 25%;
}
/* 右眼位置 */
#right-eye {
right: 25%;
}
/* 初始眨眼动画 */
@keyframes blink {
0%, 80%, 100% {
width: 12%;
height: 40%;
top: 30%;
}
98% {
width: 12%;
height: 5%;
top: 49%;
}
}
/* 睡眠状态样式 */
#robot-face.sleep .eye {
animation: none !important;
height: 5% !important;
width: 18%;
top: 49% !important;
border-radius: 45px !important;
}
#robot-face.sleep #left-eye {
left: 22% !important;
}
#robot-face.sleep #right-eye {
right: 22% !important;
}
/* 过渡到睡眠的动画 */
@keyframes toSleep {
0% {
height: 40%;
top: 30%;
border-radius: 150px;
}
100% {
height: 2%;
top: 40%;
border-radius: 45px;
}
}
.sleeping-transition .eye {
animation: toSleep 1s forwards;
}
</style>
</head>
<body>
<div id="robot-face">
<div class="eye" id="left-eye"></div>
<div class="eye" id="right-eye"></div>
</div>
<script>
const robotFace = document.getElementById('robot-face');
// 初始状态 - 眨眼
function startBlinking() {
document.querySelectorAll('.eye').forEach(eye => {
eye.style.animation = 'blink 2s infinite';
});
}
// 过渡到睡眠状态
function transitionToSleep() {
// 先停止眨眼
document.querySelectorAll('.eye').forEach(eye => {
eye.style.animation = 'none';
});
// 添加过渡动画
robotFace.classList.add('sleeping-transition');
// 过渡完成后设置为睡眠状态
setTimeout(() => {
robotFace.classList.add('sleep');
robotFace.classList.remove('sleeping-transition');
}, 2000);
}
// 自动播放动画序列
function playAnimationSequence() {
startBlinking();
// 5秒后开始过渡到睡眠状态
setTimeout(transitionToSleep, 2000);
}
// 页面加载后开始动画
window.onload = playAnimationSequence;
</script>
</body>
</html>
index.html
style.css