CSS Animation 详解

CSS Animation 允许元素平滑地从一个样式状态过渡到另一个样式状态。通过设置关键帧(keyframes),可以控制动画序列中的中间步骤。

一、核心概念

1.关键帧(Keyframes)

  1. 使用 @keyframes 规则定义动画序列
  2. 通过百分比或 from/to 指定动画阶段
  3. 每个阶段可以设置元素的样式属性

2.动画属性

  1. animation-name: 指定关键帧名称
  2. animation-duration: 动画持续时间(秒或毫秒)
  3. animation-delay: 动画延迟开始时间
  4. animation-iteration-count: 循环次数(数字或 infinite
  5. animation-direction: 动画方向(normal, reverse, alternate
  6. animation-timing-function: 速度曲线(ease, linear, ease-in-out
  7. animation-fill-mode: 动画前后的状态(forwards, backwards, both
  8. animation-play-state: 动画播放状态(running, paused

3.示例代码

下面是一个包含多种动画效果的完整示例:

html

预览

html 复制代码
<!DOCTYPE html>
<html>
<head>
<style>
/* 基础样式 */
.container {
  width: 500px;
  height: 300px;
  border: 1px solid #ccc;
  margin: 20px auto;
  position: relative;
}

/* 1. 简单移动动画 */
.box {
  width: 50px;
  height: 50px;
  background: #3498db;
  position: absolute;
  top: 20px;
  
  /* 应用动画 */
  animation: move 3s infinite;
}

@keyframes move {
  0% { left: 20px; }
  50% { left: 430px; }
  100% { left: 20px; }
}

/* 2. 旋转+透明度变化 */
.circle {
  width: 40px;
  height: 40px;
  background: #e74c3c;
  border-radius: 50%;
  position: absolute;
  top: 100px;
  left: 20px;
  
  animation: rotateAndFade 4s infinite linear;
}

@keyframes rotateAndFade {
  0% { transform: rotate(0deg); opacity: 1; }
  50% { opacity: 0.3; }
  100% { transform: rotate(360deg); opacity: 1; }
}

/* 3. 弹跳动画 */
.ball {
  width: 30px;
  height: 30px;
  background: #2ecc71;
  border-radius: 50%;
  position: absolute;
  top: 180px;
  left: 20px;
  
  animation: bounce 1s infinite alternate ease-in;
}

@keyframes bounce {
  to {
    top: 220px;
    height: 25px;
  }
}

/* 4. 悬停触发动画 */
.button {
  position: absolute;
  top: 240px;
  left: 20px;
  padding: 10px 20px;
  background: #9b59b6;
  color: white;
  border-radius: 5px;
  text-align: center;
  cursor: pointer;
  
  transition: all 0.3s;
}

.button:hover {
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* 5. 复杂动画序列 */
.combo {
  width: 40px;
  height: 40px;
  background: #f39c12;
  position: absolute;
  top: 100px;
  left: 440px;
  
  animation: combo 6s infinite;
}

@keyframes combo {
  0% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(-200px, 0) rotate(90deg); }
  50% { transform: translate(-200px, 100px) rotate(180deg); }
  75% { transform: translate(0, 100px) rotate(270deg); }
  100% { transform: translate(0, 0) rotate(360deg); }
}

</style>
</head>
<body>

<div class="container">
  <div class="box"></div>
  <div class="circle"></div>
  <div class="ball"></div>
  <div class="button">Hover Me</div>
  <div class="combo"></div>
</div>

</body>
</html>

二、高级技巧

1.多动画组合

css

css 复制代码
.element {
  animation: 
    fadeIn 1s,
    slideUp 0.8s ease-out,
    pulse 3s 2s infinite;
}
  1. 动画事件监听(JavaScript)

    javascript

    javascript 复制代码
    const element = document.querySelector('.box');
    
    element.addEventListener('animationstart', () => {
      console.log('动画开始');
    });
    
    element.addEventListener('animationend', () => {
      console.log('动画结束');
    });
    
    element.addEventListener('animationiteration', () => {
      console.log('动画循环一次');
    });
  2. 使用 cubic-bezier 自定义速度曲线

    css

    css 复制代码
    animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);

三、浏览器兼容性

大多数现代浏览器都支持 CSS Animation,但建议添加前缀以确保兼容性:

css

css 复制代码
.element {
  -webkit-animation: fadeIn 1s; /* Safari 4+ */
  -moz-animation: fadeIn 1s; /* Firefox 5+ */
  -o-animation: fadeIn 1s; /* Opera 12+ */
  animation: fadeIn 1s; /* Chrome, IE 10+ */
}

CSS Animation 为网页添加了丰富的交互体验,合理使用可以增强用户体验而不会造成干扰。

相关推荐
崔庆才丨静觅15 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606115 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了16 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅16 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅16 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅16 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment16 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅17 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊17 小时前
jwt介绍
前端
爱敲代码的小鱼17 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax