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 为网页添加了丰富的交互体验,合理使用可以增强用户体验而不会造成干扰。

相关推荐
GIS之路几秒前
OpenLayers 图形交互编辑
前端
ViceBoy_4 分钟前
前端混淆的概念-缓存&存储
前端·面试
中微子4 分钟前
从C++看JavaScript闭包:执行上下文与作用域的跨语言对比
前端·c++
前端日常开发5 分钟前
利用Web Worker实现稳定定时器:解决浏览器后台定时问题
前端
前端日常开发6 分钟前
自定义指令在 Vue.js 中的应用场景与实践
前端
_Lok6 分钟前
面试题:解释 CSS 中的 BFC 是什么,它的作用是什么?有哪些属性可以设置了会启作用?
前端
前端西瓜哥6 分钟前
平面几何:三点确定唯一圆
前端
JYeontu7 分钟前
实现一个3D轮播图
前端·javascript·css
英宋8 分钟前
ck-editor5的研究(1):快速把 CKEditor5 集成到 nuxt 中
前端·javascript
一份执念9 分钟前
前端性能优化之防抖节流
前端·性能优化