问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

CSS动画实战:打造会跳舞的小人物

创作时间:
2025-01-21 19:52:36
作者:
@小白创作中心

CSS动画实战:打造会跳舞的小人物

在网页开发中,CSS动画是一种强大的工具,可以让页面元素动起来,增加趣味性和交互性。今天,我们就来学习如何用CSS实现一个会跳舞的小人物动画。

CSS动画基础

CSS动画主要通过@keyframes规则和animation属性来实现。@keyframes用于定义动画的关键帧,而animation属性则用于将动画应用到元素上。

基本语法

/* 定义动画 */
@keyframes dance {
  0% { transform: rotate(0deg); }
  50% { transform: rotate(10deg); }
  100% { transform: rotate(0deg); }
}

/* 应用动画 */
.dancer {
  animation: dance 2s infinite;
}

在这个例子中,我们定义了一个名为dance的动画,它会让元素在2秒内从0度旋转到10度,然后再回到0度,无限循环。

制作跳舞的小人物

接下来,我们来制作一个会跳舞的小人物。首先需要搭建HTML结构:

<div class="character">
  <div class="head"></div>
  <div class="body"></div>
  <div class="arm-left"></div>
  <div class="arm-right"></div>
  <div class="leg-left"></div>
  <div class="leg-right"></div>
</div>

然后用CSS设置样式:

.character {
  position: relative;
  width: 100px;
  height: 200px;
}

.head {
  position: absolute;
  top: 0;
  left: 30px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #f00;
}

.body {
  position: absolute;
  top: 40px;
  left: 20px;
  width: 60px;
  height: 80px;
  background-color: #f00;
}

.arm-left {
  position: absolute;
  top: 80px;
  left: 0;
  width: 40px;
  height: 40px;
  background-color: #f00;
}

.arm-right {
  position: absolute;
  top: 80px;
  left: 60px;
  width: 40px;
  height: 40px;
  background-color: #f00;
}

.leg-left {
  position: absolute;
  top: 120px;
  left: 20px;
  width: 20px;
  height: 80px;
  background-color: #f00;
}

.leg-right {
  position: absolute;
  top: 120px;
  left: 60px;
  width: 20px;
  height: 80px;
  background-color: #f00;
}

现在我们已经有了一个静态的小人物,接下来让它动起来!

/* 手臂动画 */
@keyframes arm-wave {
  0% { transform: rotate(0deg); }
  50% { transform: rotate(30deg); }
  100% { transform: rotate(0deg); }
}

.arm-left, .arm-right {
  animation: arm-wave 1s infinite alternate;
}

/* 腿部动画 */
@keyframes leg-kick {
  0% { transform: rotate(0deg); }
  50% { transform: rotate(10deg); }
  100% { transform: rotate(0deg); }
}

.leg-left, .leg-right {
  animation: leg-kick 1s infinite alternate;
}

最终效果

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Dancing Character</title>
<style>
  .character {
    position: relative;
    width: 100px;
    height: 200px;
  }

  .head {
    position: absolute;
    top: 0;
    left: 30px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: #f00;
  }

  .body {
    position: absolute;
    top: 40px;
    left: 20px;
    width: 60px;
    height: 80px;
    background-color: #f00;
  }

  .arm-left {
    position: absolute;
    top: 80px;
    left: 0;
    width: 40px;
    height: 40px;
    background-color: #f00;
  }

  .arm-right {
    position: absolute;
    top: 80px;
    left: 60px;
    width: 40px;
    height: 40px;
    background-color: #f00;
  }

  .leg-left {
    position: absolute;
    top: 120px;
    left: 20px;
    width: 20px;
    height: 80px;
    background-color: #f00;
  }

  .leg-right {
    position: absolute;
    top: 120px;
    left: 60px;
    width: 20px;
    height: 80px;
    background-color: #f00;
  }

  /* 动画 */
  @keyframes arm-wave {
    0% { transform: rotate(0deg); }
    50% { transform: rotate(30deg); }
    100% { transform: rotate(0deg); }
  }

  .arm-left, .arm-right {
    animation: arm-wave 1s infinite alternate;
  }

  @keyframes leg-kick {
    0% { transform: rotate(0deg); }
    50% { transform: rotate(10deg); }
    100% { transform: rotate(0deg); }
  }

  .leg-left, .leg-right {
    animation: leg-kick 1s infinite alternate;
  }
</style>
</head>
<body>
<div class="character">
  <div class="head"></div>
  <div class="body"></div>
  <div class="arm-left"></div>
  <div class="arm-right"></div>
  <div class="leg-left"></div>
  <div class="leg-right"></div>
</div>
</body>
</html>

通过这个简单的例子,我们可以看到CSS动画的强大功能。只需要几行代码,就能让页面元素动起来,为网页增添趣味性和互动性。希望这个教程能激发你对CSS动画的兴趣,尝试创作更多有趣的动画效果!

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号