返回文章列表

CSS 动画详解:transform、transition 与 animation

CSS 动画是现代 Web 开发中不可或缺的技能。通过 CSS 原生的动画能力,我们可以在不依赖 JavaScript 的情况下创建流畅、高性能的视觉动效。本文将深入讲解 CSS 动画的三大核心技术:transform(变换)、transition(过渡)和 animation(关键帧动画),并通过丰富的实战案例帮助你掌握它们。

一、transform:CSS 变换

transform 属性用于对元素进行几何变换,包括平移、旋转、缩放和倾斜。它不会影响文档流中的其他元素,也不会触发页面重排(reflow),性能非常优秀。

1.1 translate:平移

.box {
    width: 100px;
    height: 100px;
    background: #ff6b6b;
    transition: transform 0.3s ease;
}

/* 水平平移 */
.box:hover {
    transform: translateX(50px);
}

/* 垂直平移 */
.box:hover {
    transform: translateY(-20px);
}

/* 同时在两个方向平移 */
.box:hover {
    transform: translate(50px, -20px);
}

/* 百分比相对于元素自身大小 */
.box:hover {
    transform: translateX(100%); /* 向右移动自身宽度 */
}

/* translate3d 可以开启 GPU 硬件加速 */
.box:hover {
    transform: translate3d(50px, -20px, 0);
}

1.2 rotate:旋转

.box {
    transition: transform 0.5s ease;
}

/* 顺时针旋转 45 度 */
.box:hover {
    transform: rotate(45deg);
}

/* 逆时针旋转 */
.box:hover {
    transform: rotate(-45deg);
}

/* 绕 X 轴旋转(3D) */
.box:hover {
    transform: rotateX(180deg);
}

/* 绕 Y 轴旋转(3D) */
.box:hover {
    transform: rotateY(180deg);
}

1.3 scale:缩放

.box {
    transition: transform 0.3s ease;
}

/* 等比放大 1.5 倍 */
.box:hover {
    transform: scale(1.5);
}

/* 缩小到一半 */
.box:hover {
    transform: scale(0.5);
}

/* 分别设置水平和垂直缩放 */
.box:hover {
    transform: scaleX(2) scaleY(0.5);
}

/* 只在 X 方向缩放 */
.box:hover {
    transform: scaleX(1.5);
}

1.4 skew:倾斜

.box {
    transition: transform 0.3s ease;
}

/* 沿 X 轴倾斜 30 度 */
.box:hover {
    transform: skewX(30deg);
}

/* 沿 Y 轴倾斜 */
.box:hover {
    transform: skewY(10deg);
}

/* 同时设置两个方向 */
.box:hover {
    transform: skew(15deg, 10deg);
}

1.5 变换原点

/* 变换原点默认是元素中心 */
.box {
    transform-origin: center center;
}

/* 从左上角变换 */
.box {
    transform-origin: top left;
}

/* 使用具体数值 */
.box {
    transform-origin: 0 0; /* 左上角 */
    transform-origin: 100% 100%; /* 右下角 */
}

/* 从左下角旋转(常见于展开菜单效果) */
.menu {
    transform-origin: left bottom;
    transform: rotate(-90deg);
}
.menu.open {
    transform: rotate(0deg);
}

1.6 组合多个变换

/* 多个变换可以组合使用,顺序从右到左执行 */
.card:hover {
    transform: translateX(10px) translateY(-5px) rotate(3deg) scale(1.05);
}

/* 也可以使用矩阵变换(高级用法) */
.box {
    transform: matrix(1, 0.5, -0.5, 1, 0, 0);
}

/* 设置 3D 透视效果 */
.scene {
    perspective: 800px; /* 父元素设置透视距离 */
}
.cube {
    transform-style: preserve-3d;
    transform: rotateX(30deg) rotateY(45deg);
}

二、transition:CSS 过渡

transition 属性用于在元素状态变化时创建平滑的过渡效果。它需要两个条件:一是要有状态变化(如 hover、class 切换),二是要有可以过渡的属性值。

2.1 基本语法

/* 完整语法:transition: property duration timing-function delay */
.button {
    background: #ff6b6b;
    color: #fff;
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
}

.button:hover {
    background: #ee5a24;
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(238, 90, 36, 0.4);
}

2.2 过渡属性详解

.element {
    /* transition-property: 指定要过渡的属性 */
    transition-property: background-color, transform, opacity;

    /* transition-duration: 过渡持续时间 */
    transition-duration: 0.3s, 0.5s, 0.2s;

    /* transition-timing-function: 过渡时间函数 */
    transition-timing-function: ease;

    /* transition-delay: 过渡延迟 */
    transition-delay: 0.1s;
}

/* 简写(推荐使用) */
.element {
    /* property duration timing-function delay */
    transition: background-color 0.3s ease 0.1s,
                transform 0.5s ease-in-out,
                opacity 0.2s linear;
}

/* 所有属性都使用相同过渡 */
.element {
    transition: all 0.3s ease; /* 注意:all 可能影响性能 */
}

2.3 时间函数(timing-function)

/* 常用时间函数 */
.element {
    /* ease(默认):开始快,结束慢 */
    transition-timing-function: ease;

    /* linear:匀速 */
    transition-timing-function: linear;

    /* ease-in:开始慢,结束快 */
    transition-timing-function: ease-in;

    /* ease-out:开始快,结束慢 */
    transition-timing-function: ease-out;

    /* ease-in-out:开始和结束都慢,中间快 */
    transition-timing-function: ease-in-out;

    /* 贝塞尔曲线(完全自定义) */
    /* cubic-bezier(x1, y1, x2, y2) */
    transition-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55); /* 弹跳效果 */
    transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94); /* 流畅减速 */
}

/* 弹跳过渡示例 */
.bounce-hover {
    transition: transform 0.5s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}
.bounce-hover:hover {
    transform: scale(1.1);
}

2.4 可过渡的属性

并非所有 CSS 属性都可以过渡。只有具有中间值的属性才能过渡,常见的包括:

三、@keyframes 与 animation 属性

与 transition 不同,animation 可以在不需要触发条件的情况下自动播放复杂动画。它由 @keyframes 定义关键帧,然后用 animation 属性控制播放。

3.1 定义关键帧

/* 基本语法:使用百分比定义关键帧 */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* 等价写法 */
@keyframes fadeIn {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

/* 多个关键帧 */
@keyframes bounce {
    0% {
        transform: translateY(0);
    }
    25% {
        transform: translateY(-30px);
    }
    50% {
        transform: translateY(0);
    }
    75% {
        transform: translateY(-15px);
    }
    100% {
        transform: translateY(0);
    }
}

/* 同时变换多个属性 */
@keyframes slideIn {
    0% {
        transform: translateX(-100%);
        opacity: 0;
    }
    60% {
        transform: translateX(10px); /* 略微过冲 */
        opacity: 1;
    }
    100% {
        transform: translateX(0);
        opacity: 1;
    }
}

3.2 animation 属性详解

.animated-element {
    /* animation-name: 关键帧名称 */
    animation-name: bounce;

    /* animation-duration: 动画持续时间 */
    animation-duration: 1s;

    /* animation-timing-function: 时间函数 */
    animation-timing-function: ease-in-out;

    /* animation-delay: 播放延迟 */
    animation-delay: 0.5s;

    /* animation-iteration-count: 播放次数 */
    animation-iteration-count: infinite; /* 或具体次数如 3 */

    /* animation-direction: 播放方向 */
    animation-direction: normal;    /* 正向 */
    /* animation-direction: reverse;     反向 */
    /* animation-direction: alternate;  交替(奇数次正向,偶数次反向) */
    /* animation-direction: alternate-reverse; */

    /* animation-fill-mode: 动画结束后的状态 */
    animation-fill-mode: forwards; /* 保持最后一帧的状态 */
    /* animation-fill-mode: backwards;  动画开始前应用第一帧 */
    /* animation-fill-mode: both;       同时应用 forwards 和 backwards */

    /* animation-play-state: 播放状态 */
    animation-play-state: running; /* 或 paused */
}

/* 简写(推荐) */
.animated-element {
    /* name duration timing-function delay iteration-count direction fill-mode play-state */
    animation: bounce 1s ease-in-out 0.5s infinite alternate forwards;
}

/* 更常见的简写 */
.animated-element {
    animation: fadeIn 0.5s ease forwards;
    animation: bounce 1s ease infinite;
    animation: slideIn 0.8s cubic-bezier(0.68, -0.55, 0.27, 1.55) forwards;
}

3.3 多动画组合

/* 可以同时应用多个动画,用逗号分隔 */
.element {
    animation: fadeIn 0.5s ease forwards,
               slideUp 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.3s forwards,
               pulse 2s ease-in-out 1s infinite;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from { transform: translateY(30px); }
    to { transform: translateY(0); }
}

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

四、实战案例

4.1 按钮悬停效果

<button class="btn-hover">悬停试试</button>
<button class="btn-glow">发光按钮</button>
<button class="btn-slide">滑入按钮</button>
/* 基础悬停上浮效果 */
.btn-hover {
    padding: 12px 32px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
                box-shadow 0.3s ease;
}

.btn-hover:hover {
    transform: translateY(-3px) scale(1.02);
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
}

.btn-hover:active {
    transform: translateY(-1px) scale(0.98);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
}

/* 发光效果 */
.btn-glow {
    padding: 12px 32px;
    background: #ff6b6b;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    position: relative;
    transition: box-shadow 0.3s ease, transform 0.3s ease;
    overflow: hidden;
}

.btn-glow:hover {
    box-shadow: 0 0 20px rgba(255, 107, 107, 0.6),
                0 0 40px rgba(255, 107, 107, 0.4),
                0 0 60px rgba(255, 107, 107, 0.2);
    transform: scale(1.05);
}

/* 背景滑动效果 */
.btn-slide {
    padding: 12px 32px;
    background: transparent;
    color: #4ecdc4;
    border: 2px solid #4ecdc4;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: color 0.4s ease;
    z-index: 1;
}

.btn-slide::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: #4ecdc4;
    transition: left 0.4s ease;
    z-index: -1;
}

.btn-slide:hover::before {
    left: 0;
}

.btn-slide:hover {
    color: #fff;
}

4.2 加载动画(Spinner)

<div class="spinner-container">
    <div class="spinner"></div>
    <div class="spinner spinner-2"></div>
    <div class="spinner spinner-3"></div>
</div>
/* 经典旋转圆环 */
.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #e0e0e0;
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    display: inline-block;
    margin: 0 20px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* 双色旋转 */
.spinner-2 {
    border-top-color: #667eea;
    border-bottom-color: #764ba2;
    animation-duration: 1.2s;
    animation-timing-function: cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

/* 跳动点加载 */
.spinner-3 {
    width: 12px;
    height: 12px;
    border: none;
    border-radius: 50%;
    background: #ff6b6b;
    margin: 0 5px;
    animation: dotBounce 1.4s infinite ease-in-out both;
}

.spinner-3:nth-child(3) { animation-delay: -0.32s; }
.spinner-3:nth-child(4) { animation-delay: -0.16s; }

@keyframes dotBounce {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 脉冲加载 */
.pulse-loader {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #4ecdc4;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0% {
        transform: scale(0.8);
        opacity: 1;
    }
    100% {
        transform: scale(2.5);
        opacity: 0;
    }
}

4.3 淡入淡出效果

<div class="fade-demo">
    <div class="fade-in fade-up">从下方淡入</div>
    <div class="fade-in fade-left">从左侧淡入</div>
    <div class="fade-in fade-scale">缩放淡入</div>
</div>
.fade-demo {
    display: flex;
    gap: 20px;
    padding: 40px;
}

.fade-in {
    padding: 20px 40px;
    background: #1a1a2e;
    color: #fff;
    border-radius: 8px;
    opacity: 0;
    animation-fill-mode: forwards;
    animation-duration: 0.6s;
    animation-timing-function: ease-out;
}

/* 从下方淡入 */
.fade-up {
    transform: translateY(30px);
    animation-name: fadeUp;
    animation-delay: 0.1s;
}

/* 从左侧淡入 */
.fade-left {
    transform: translateX(-30px);
    animation-name: fadeLeft;
    animation-delay: 0.3s;
}

/* 缩放淡入 */
.fade-scale {
    transform: scale(0.8);
    animation-name: fadeScale;
    animation-delay: 0.5s;
}

@keyframes fadeUp {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeLeft {
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes fadeScale {
    to {
        opacity: 1;
        transform: scale(1);
    }
}

4.4 3D 翻转卡片

<div class="card-3d">
    <div class="card-3d-inner">
        <div class="card-3d-front">
            <h3>正面内容</h3>
            <p>悬停查看背面</p>
        </div>
        <div class="card-3d-back">
            <h3>背面内容</h3>
            <p>这是翻转后的背面</p>
        </div>
    </div>
</div>
.card-3d {
    width: 300px;
    height: 200px;
    perspective: 1000px; /* 设置透视距离 */
    margin: 40px auto;
    cursor: pointer;
}

.card-3d-inner {
    width: 100%;
    height: 100%;
    position: relative;
    transition: transform 0.6s ease;
    transform-style: preserve-3d; /* 子元素保持 3D 效果 */
}

.card-3d:hover .card-3d-inner {
    transform: rotateY(180deg); /* 悬停时绕 Y 轴翻转 180 度 */
}

.card-3d-front,
.card-3d-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden; /* 背面不可见 */
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.card-3d-front {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
}

.card-3d-back {
    background: linear-gradient(135deg, #ff6b6b, #ee5a24);
    color: #fff;
    transform: rotateY(180deg); /* 背面初始翻转 180 度 */
}

五、性能优化建议

/* 性能优化示例 */
.animated {
    will-change: transform, opacity; /* 提示浏览器优化 */
    transform: translateZ(0); /* 开启 GPU 层 */
}

/* 尊重用户的减少动画偏好 */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

六、总结

CSS 动画三大核心技术各有适用场景:

在实际开发中,三者经常组合使用。例如用 @keyframes 控制动画序列,用 transform 执行变换操作,用 transition 处理用户交互的即时反馈。掌握这三者,你就拥有了创建流畅、优雅的网页动效的核心能力。

动手挑战

学到这里,不妨动手试一试以下练习,巩固你的理解:

  1. 基础练习:回顾本文核心概念,用自己的话总结关键知识点。
  2. 进阶实践:将文中的示例代码运行一遍,尝试修改参数观察变化。
  3. 拓展思考:想一想这个技术/方法还能应用在哪些场景中?

小贴士:遇到问题时,先独立思考,再查阅资料,最后请教他人——这是成长最快的学习方式。

赞赏支持

本文更新于 2026-08-22,环境 Python 3.12