搜索

CSS 教学

基本介绍

认识 CSS 开始使用 CSS CSS 语法规则 CSS 命名原则 CSS 常用样式属性

CSS 选择器

认识 CSS 选择器 优先级 ( 权重 ) 样式继承与联集 使用巢状结构语法 元素选择器 ID 和 Class 选择器 属性选择器 文件结构选择器 虚拟类别选择器 ( 结构 ) 虚拟类别选择器 ( 类型 ) 虚拟类别选择器 ( 输入 ) 虚拟类别选择器 ( 行为 ) 虚拟类别选择器 ( 超链接 ) 虚拟类别选择器 ( 逻辑 ) 虚拟类别选择器 ( 其他 ) 虚拟元素选择器 群组与组合选择器

数值与单位

全域关键字与文字数值 长度与角度单位 位置名称与时间单位 特殊样式属性 ( all、appearance)

规则与定义

变数 ( Variables ) 媒体查询 ( @media ) 容器查询 ( @container ) 自订属性值 ( @property ) 导入样式 ( @import ) 分层优先级 ( @layer ) 选择器作用域 ( @scope ) 定义字型 ( @font-face ) 计数规则 ( @counter-style ) 打印网页 ( @page )

函数类型

数学计算 文字与清单计数 形状与线段 路径 ( path )

颜色与滤镜

颜色单位 色彩模型 相对颜色 渐层色 图像滤镜 ( filter ) 背景滤镜 ( backdrop-filter ) 混合模式 ( mix-blend-mode )

文字与段落

设定字型 ( font-family ) 使用外部字型 文字尺寸 文字样式 ( 常用 ) 文字样式 ( 其他实用 ) 文字样式 ( 特殊用途 ) 文字换行 文字空白与 Tab 大小 文字行高与缩排 文字水平与垂直对齐 文字书写方向 文字自动分栏 使用 Color Font ( 彩色字型 ) 使用 Icon Font ( 图示文字 ) 制作自己的 Icon Font 显示生成内容与引号

元素容器

容器显示类型 ( display ) 元素 display 对照表 盒子模型 ( Box Model ) 宽高尺寸 ( width、height ) 显示与比例 ( box-sizing、aspect-ratio ) 内边距 ( padding ) 外边界 ( margin ) 边框 ( border ) 边框圆角 ( border-radius ) 图像边框 ( border-image ) 轮廓 ( outline ) 内容溢出与裁切 ( overflow ) 内容范围 ( contain ) 可见性与透明度 缩放与调整 ( zoom、resize )

背景与阴影

背景 ( 背景色、背景裁切 ) 背景图 ( 定位、尺寸 ) 背景图 ( 固定、重复 ) 背景图 ( 多重背景、混合 ) 背景缩写 ( background ) 容器阴影 ( box-shadow )

清单与表格

清单样式 清单计数器 表格基本样式 表格边框重叠与间距 表格内容宽度与对齐

基本排版与定位

元素排版方式 浮动 ( float ) 浮动形状 ( shape-* ) 定位 ( position ) 逻辑属性 ( logical properties ) 重置 CSS ( Reset CSS ) 水平置中技巧 垂直置中技巧

Flex 弹性排版

Flexbox 弹性盒子 Flex 对齐方式 Flex 弹性伸缩

Grid 网格排版

Grid 网格容器与格线 Grid 网格空间与命名 Grid 网格流向与间距 Grid 排列网格项目 Grid 项目对齐与顺序

转场与动画

转场 ( transition ) 转场触发事件 动画 ( animation ) 自订动画路径 ( offset ) 动画触发事件 多重动画的权重与顺序 动画进度控制 ( timeline )

变形、裁切与遮罩

裁切路径 ( clip-path ) 图像遮罩 ( mask ) 对象填满方式与定位 转换函数 ( transform ) 平移、旋转与缩放 3D 转换与透视

视窗与用户行为

卷轴样式 ( scrollbar ) 拉霸、滑杆样式 ( slider ) 鼠标光标图示 ( cursor ) 鼠标事件 ( pointer-events ) 用户选取 ( user-select ) 卷动行为 ( scroll、overscroll ) 打印换页 ( break-* )

CSS 预处理器

认识 CSS 预处理器 Less ( 安装、开始使用 ) Less ( 巢状结构、选择器 ) Less ( 变数、import ) Less ( Extend、Mixin ) Less ( 逻辑、循环、函数 )

范例效果

CSS 多重绕圈 3D 动画 CSS 圆饼图 CSS 跑马灯 Google 加载动画 电子时钟数字 渐层色的转场与动画 渐层色制作星空背景 渐层色边框 渐层色制作图案背景 不规则形状动画 弹跳的正方形动画 3D 正多面体 超链接底线动画效果 恶心黏黏球效果 CSS 视差滚动效果 卷轴控制放射形选单 卷轴改变文字背景色 CSS 页面卷动进度条 CSS 水波效果 圆点加载动画 ( 阴影动画 ) 字母翻牌效果 探照灯动画 立体双色跑马灯 楼梯文字动态效果 渐层色文字、双色文字 流动色彩文字 空心文字、文字水波背景 左右倾斜的背景效果 Icon 图示形状变换 故障文字效果 按钮的多种 hover 效果 点击展开或收折的选单 点击爱心图案放射效果 纯 CSS 数字加载进度条 纯 CSS 码表 ( 可计时 )

动画 ( animation )

随着浏览器对于 CSS 的支援度提升,现在已经可以透过 CSS 做出许多酷炫的“动画”效果,这篇教学会先从 CSS 的 @keyframes 定义动画影格,逐一介绍 CSS animation 各个样式属性,例如 animation-name、animation-duration、animation-delay、animation-timing-function、animation-direction...等,透过这些动画样式属性实作 CSS 动画。

阅读更多:CSS animation 触发事件、自订 CSS 动画路径 offset、CSS 多重动画的权重与顺序

快速导览:

CSS 教学 - 动画 animation

@keyframe 定义动画影格

要使用 CSS 制作动画,必须先透过 @keyframe 定义动画的影格,@keyframe 的基本写法需要包含“name、from、to”三个关键字,基本写法如下:

@keyframe 关键字 说明
name 这组影格的名称。
from 起始样式的属性值。
to 结束样式的属性值。
@keyframes name {
  from {
    樣式: 屬性值;
  }
  to {
    樣式: 屬性值;
  }
}

定义名称关键字 name 时,可使用英文字母大小写、数字、连接线“-”或底线“_”,并有下列几点需要注意:

  • 名称开头需为英文字母 ( 可用单一底线开头 )。
  • 名称大小写有区别。
  • 名称可以加引号、亦可不加引号。
  • 如果有两组 @keyframes,同名,以后面的名称为主。
  • 不能使用 None 或 initial 作为名称 ( 这两个是 CSS 保留字 ),如果真的要使用,需要使用引号包覆,例如 "None" 或 "initial"。
@keyframes oxxo { ... }
@keyframes "oxxo" { ... }    /* 位於下方,若和上方同名,則會採用這個 */
@keyframes OXXO { ... }      /* 大小寫與 oxxo 不同,屬於完全不同的影格定義 */

@keyframes initial { ... }   /* 不可使用 */
@keyframes None { ... }      /* 不可使用 */
@keyframes "initial" { ... } /* 可以用 */
@keyframes "None" { ... }    /* 可以用 */

下方范例使用 @keyframe 定义了一组名为为 oxxo 的影格,并指定起始的 width 和 height 样式属性值为 50px,结束的 width 和 height 样式属性值为 200px,借着透过 animation-name 执行这组影格,animation-duration 定义这组影格渐变的时间,网页执行后,当鼠标移动到红色方块上方,红色方块就会变大,动画结束后会回复原本的样式设定。

线上展示:https://codepen.io/oxxo/pen/GRVevMr

<!-- HTML 程式碼-->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div{
    width:50px;
    height:50px;
    background:#f00;
  }
  div:hover {
    animation-name: oxxo;   /* 使用 oxxo 影格定義執行動畫 */
    animation-duration: 2s; /* 動畫時間為 2 秒 */
  }
  @keyframes oxxo{
    from{
      width: 50px;
      height: 50px;
    }
    to{
      width: 200px;
      height: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - @keyframe 定义动画影格

除了使用 @keyframe 关键字 from 和 to,也可以使用“百分比”的方式增加动画过程中的关键影格,百分比数值和关键字可以混合使用,关键字 from 等同“0%”,to 等同“100%”,下方范例会使用百分比的方式,在动画过程中加入 33% 与 66% 两组关键影格,网页执行后会看见红色方块先改变 width,再改变 height,最后又会回到原本的大小。

注意,关键影格的 0% 必须要有“%”单位。

线上展示:https://codepen.io/oxxo/pen/XWvGaeG

<!-- HTML 程式碼-->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div{
    width:50px;
    height:50px;
    background:#f00;
  }
  div:hover {
    animation-name: oxxo;
    animation-duration: 4s;
  }
  @keyframes oxxo{
    from, to{
      width: 50px;
      height: 50px;  /* 因為 from 和 to 內容相同,可使用 CSS 的寫法寫在一起 */
    }
    33% {
      width: 200px;
      height: 50px;  /* 如果沒有寫這一段,則會出現 0% 到 66% 的高度變化 */
    }
    66% {
      width: 200px;
      height: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - @keyframe 使用百分比

如果省略了 from、0% 或 to、100%,就会使用元素“原本的样式”替代,下方的范例省略了 from 和 to,产生的结过就与上方范例完全相同。

线上展示:https://codepen.io/oxxo/pen/GRVevOr

<!-- HTML 程式碼-->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div{
    width:50px;
    height:50px;
    background:#f00;
  }
  div:hover {
    animation-name: oxxo;
    animation-duration: 4s;
  }
  @keyframes oxxo{
    33% {
      width: 200px;
      height: 50px;  /* 如果沒有寫這一段,則會出現 0% 到 66% 的高度變化 */
    }
    66% {
      width: 200px;
      height: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - @keyframe 省略 from 和 to

注意,能够在动画效果中加入补间动画数值的样式属性,需要具备能“计算”的属性值,例如颜色、位置、尺寸、角度、透明度...等,如果是使用“关键字”作为属性值的样式,则无法加入补间动画效果,例如 none、solid、dashed...等,此外,若支援动画的样式使用 auto、max-content 等关键字作为属性值 ( 或不设定任何属性值 ),也会无法顺利产生补间动画 ( 或产生不如预期的效果 )。

下方范例使用 max-content 和 auto 作为属性值,会产生不如预期的效果 ( 结束时区块消失 )。

线上展示:https://codepen.io/oxxo/pen/zYgbdPQ

<!-- HTML 程式碼-->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div{
    width: max-content;  /* 使用 max-content */
    height: auto;        /* 使用 auto */
    background:#f00;
  }
  div:hover {
    animation-name: oxxo;
    animation-duration: 4s;
  }
  @keyframes oxxo{
    33% {
      width: 200px;
      height: 50px;
    }
    66% {
      width: 200px;
      height: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - @keyframe 不支援使用关键字作为属性值

animation-name 动画名称

animation-name 表示“动画名称”的样式属性,没有继承特性,这个样式属性会执行“特定名称的影格定义”,是操作 CSS 动画时的“必要”样式属性,需要注意使用时名称“不能加上引号”。

div {
  animation-name: oxxo;
}
@keyframes oxxo{
  50% {background: green;}
}

animation-duration 动画持续时间

animation-duration 表示“动画持续时间”的样式属性,换句话说就是“from 到 to”要经过的时间,没有继承特性,使用非负值的数值,数值需要搭配“s”( 秒 ) 或“ms”( 1/1000 秒、毫秒 ) 等时间单位,是操作 CSS 动画时的“必要”样式属性。

div {
  animation-name: oxxo;
  animation-duration: 1s;  /* 動畫總時長 1 秒 */
}
@keyframes oxxo{
  50% {background: green;}
}

animation-iteration-count 动画播放次数

animation-iteration-count 表示“动画播放的次数”,默认值为 1 次,没有继承特性,如果设定为 infinite 动画就会无止尽的播放下去。

div {
  animation-name: oxxo;
  animation-duration: 1s;
  animation-iteration-count: 2;  /* 播放兩次 */
}
@keyframes oxxo{
  50% {background: green;}
}

animation-delay 动画延迟时间

animation-delay 表示“动画延迟时间”,没有继承特性,需要搭配“s”( 秒 ) 或“ms”( 1/1000 秒、毫秒 ) 等时间单位,如果没有设定或将其设为 0s,动画就会直接播放不会延迟。如果将延迟播放时间设定为“负值” ( 例如 -1s、-2s ),就会产生“快转”的效果,假设一段动画持续时间为 5 秒,animation-delay 设定为 -2s,动画将会直接从第二秒的位置开始播放,播放三秒后停止 ( 5-2=3 )。

下方范例有三个不同颜色的方块,都套用同样的 oxxo 影格定义,动画也都设定为持续 5 秒,但是蓝色的 animation-delay 设为 2s,红色的 animation-delay 设为 -2s,绿色则完全不设定 animation-delay,执行后蓝色会慢两秒移动,红色会直接跳到已经移动两秒的地方再开始移动,绿色则是一开始就移动。

线上展示:https://codepen.io/oxxo/pen/MWNxvrz

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方開始動畫</h2>
<div class="r"></div>
<div class="g"></div>
<div class="b"></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 50px;
    height: 50px; 
    margin: 10px;
  }
  h2:hover~div{
    animation-name: oxxo;
    animation-duration: 5s;
  }
  .r{
    background: #f00;
    animation-delay: -2s;  /* 快轉 */
  }
  .g{background: #0a0;}
  .b{
    background: #00c;
    animation-delay: 2s;   /* 延遲 */
  }

  @keyframes oxxo {
    from {margin-left: 10px;}
    to {margin-left: 200px;}
  }
</style>

CSS 教学 - 动画 animation - animation-delay 动画延迟播放时间

animation-timing-function 动画速度

animation-timing-function 表示“动画的速度”,默认值为 ease,没有继承特性,这个样式属性可以让动画播放的“每个关键影格之间”具有“加速、减速、平缓加速、线性移动”等速度变化,根据“W3C CSS Easing Functions Level 1”的说明,共有下列几种属性值:

属性值 说明
linear 线性,没有加减速。
ease-in 加速开始,线性停止。
ease-out 线停开始,减速停止。
ease-in-out 加速开始,减速停止。
ease 加速开始,减速停止。
step-start 维持在 0% 样式,等待动画时间过后,直接跳到 100% 样式。
step-end 直接跳到 100% 样式,并等待动画时间过完。
steps(n, jump-start) 分成 n 个步骤跳往 100% 样式。
steps(n, jump-end) 分成 n 个步骤跳往 100% 样式。
steps(n, jump-none) 分成 n-1 个步骤跳往 100% 样式。
steps(n, jump-both) 分成 n+1 个步骤跳往 100% 样式。

CSS 教学 - 动画 animation - animation-timing-function 动画速度

下方范例会让不同的 div 从左移动到右再从右移动到左,从中可以观察不同动画速度的效果。

线上展示:https://codepen.io/oxxo/pen/mdNoMXR

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<h3>linear</h3><div></div>
<h3>ease-in</h3><div></div>
<h3>ease-out</h3><div></div>
<h3>ease-in-out</h3><div></div>
<h3>ease</h3><div></div>
<h3>step-start</h3><div></div>
<h3>step-end</h3><div></div>
<h3>steps(5, jump-start)</h3><div></div>
<h3>steps(5, jump-end)</h3><div></div>
<h3>steps(5, jump-none)</h3><div></div>
<h3>steps(5, jump-both)</h3><div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {border: 2px solid #000;}
  h2:hover~div {
    animation-name: oxxo;
    animation-duration: 5s;
  }
  h3 {
    margin:5px;
    width: 190px;
    height: 30px;
    float:left;
    clear: both;
    text-align: right;
  }
  div {
    float:left;
    width: 30px;
    height: 30px;
    margin:5px;
    background: red;
  }
  div:nth-of-type(1) {animation-timing-function: linear;}
  div:nth-of-type(2) {animation-timing-function: ease-in;}
  div:nth-of-type(3) {animation-timing-function: ease-out;}
  div:nth-of-type(4) {animation-timing-function: ease-in-out;}
  div:nth-of-type(5) {animation-timing-function: ease;}
  div:nth-of-type(6) {animation-timing-function: step-start;}
  div:nth-of-type(7) {animation-timing-function: step-end;}
  div:nth-of-type(8) {animation-timing-function: steps(5, jump-start);}
  div:nth-of-type(9) {animation-timing-function: steps(5, jump-end);}
  div:nth-of-type(10) {animation-timing-function: steps(5, jump-none);}
  div:nth-of-type(11) {animation-timing-function: steps(5, jump-both);}

  @keyframes oxxo{
    50% {margin-left: 200px;}
  }
</style>

CSS 教学 - 动画 animation - animation-timing-function 动画速度

除了使用默认的动画速度,也可以使用 CSS 内建函数 cubic-bezier 设定“客制化的动画速度”,cubic-bezier 是一个可调整的“贝兹曲线”,总共有四个参数,分别是第一个贝兹曲线控制点 p1 的座标 (x1, y1) 和第二个贝兹曲线控制点 p2 的座标 (x2, y2) ( 参考 Cubic Bézier )。

CSS 教学 - 动画 animation - cubic-bezier

透过线上工具“cubic-bezier.com”可以使用视觉化的方式,拖拉这两个控制点,调整控制点之后,上方就会看见这个动画速度的写法。

CSS 教学 - 动画 animation - 线上工具 cubic-bezier.com

下方范例会透过 cubic-bezier,产生四种有趣的动画速度,从中可以看到当设定值超过范围时,甚至会产生超过原本边界的特殊效果。

线上展示:https://codepen.io/oxxo/pen/qBevXoE

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<h3>cubic-bezier(.24,1.19,.68,-0.07)</h3><div></div>
<h3>cubic-bezier(.96,.34,0,.73)</h3><div></div>
<h3>cubic-bezier(.46,-0.75,.18,1.49)</h3><div></div>
<h3>cubic-bezier(.41,.86,.55,2.08)</h3><div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {border: 2px solid #000;}
  h2:hover~div {
    animation-name: oxxo;
    animation-duration: 5s;
  }
  h3 {
    margin:5px;
    width: 290px;
    height: 30px;
    float:left;
    clear: both;
    text-align: right;
  }
  div {
    float:left;
    width: 30px;
    height: 30px;
    margin:5px;
    background: red;
  }
  div:nth-of-type(1) {animation-timing-function: cubic-bezier(.24,1.19,.68,-0.07);}
  div:nth-of-type(2) {animation-timing-function: cubic-bezier(.96,.34,0,.73);}
  div:nth-of-type(3) {animation-timing-function: cubic-bezier(.46,-0.75,.18,1.49);}
  div:nth-of-type(4) {animation-timing-function: cubic-bezier(.41,.86,.55,2.08);}

  @keyframes oxxo{
    50% {margin-left: 200px;}
  }
</style>

CSS 教学 - 动画 animation - cubic-bezier 效果

如果熟练 steps 的用法,就能够很简单的使用“sprite 图片”来做动画,sprite 图片就是将许多图案集合成一张图,接着透过 CSS 的语法使这些图案分别呈现在网页里,大幅减少多张图片加载的 request 数量,下方范例会使用一张经典的 sprite 图片 ( Leland Stanford 所拍摄 ),透过 CSS 动画的 steps 就能让图片中的马儿跑起来。

图片下载:Leland Stanford 马儿图

CSS 教学 - 动画 animation - Leland Stanford 马儿图

线上展示:https://codepen.io/oxxo/pen/jOgJLzK

<!-- HTML 程式碼-->
<div></div>

<!-- CSS 程式碼 -->
<style>
  div {
    width: 186px;
    height: 141px; 
    background-image: url("https://steam.oxxostudio.tw/download/css/animation-horse.jpg");
    animation-name: oxxo;
    animation-duration: 1s;
    animation-iteration-count: infinite;
    animation-timing-function: step-start;  /* 使用 step-start */
  }

  @keyframes oxxo{
    0% {background-position: -15px -13px;}
    6.25% {background-position: -210px -13px;}
    12.5% {background-position: -403px -13px;}
    18.75% {background-position: -592px -13px;}
    25% {background-position: -15px -165px;}
    31.25% {background-position: -210px -165px;}
    37.5% {background-position: -403px -165px;}
    43.75% {background-position: -592px -165px;}
    50% {background-position: -15px -320px;}
    56.25% {background-position: -210px -320px;}
    62.5% {background-position: -403px -320px;}
    68.75% {background-position: -592px -320px;}
    75% {background-position: -15px -470px;}
    81.25% {background-position: -210px -470px;}
    87.5% {background-position: -403px -470px;}
    93.75% {background-position: -592px -470px;}
    100 %{background-position: -592px -470px;}
  }
</style>

CSS 教学 - 动画 animation - 使用 steps 搭配 sprite 图片做动画

animation-direction 动画播放方向

animation-direction 表示“动画播放方向”的样式属性,没有继承特性,有下列几种属性值:

属性值 说明
normal 正常播放,从 0% 到 100% ( 默认值 )。
reverse 反转播放,从 100% 到 0%。
alternate 正反转轮流播放,奇数次为 0% 到 100%,偶数次为 100% 到 0%,若动画播放次数只有一次就只会正常播放。
alternate-reverse 正反转轮流播放,奇数次为 100% 到 0%,偶数次为 0% 到 100%,若动画播放次数只有一次就只会反转播放。

下方范例会展示这四种播放方向的效果。

线上展示:https://codepen.io/oxxo/pen/qBevXYd

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<h3>normal</h3><div></div>
<h3>reverse</h3><div></div>
<h3>alternate</h3><div></div>
<h3>alternate-reverse</h3><div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {border: 2px solid #000;}
  h2:hover~div {
    animation-name: oxxo;
    animation-duration: 2s;
    animation-iteration-count: 4;
  }
  h3 {
    margin:5px;
    width: 190px;
    height: 30px;
    float:left;
    clear: both;
    text-align: right;
  }
  div {
    float:left;
    width: 30px;
    height: 30px;
    margin:5px;
    background: red;
  }
  div:nth-of-type(1) {animation-direction:normal;}
  div:nth-of-type(2) {animation-direction:reverse;}
  div:nth-of-type(3) {animation-direction:alternate;}
  div:nth-of-type(4) {animation-direction:alternate-reverse;}

  @keyframes oxxo{
    0% {margin-left: 5px;}
    100% {margin-left: 200px;}
  }
</style>

CSS 教学 - 动画 animation - animation-direction 动画播放方向

animation-fill-mode 动画播放前后模式

animation-fill-mode 表示“动画播放前后模式”的样式属性,也就是动画完成后要停留在哪个样式,没有继承特性,有下列几种属性值:

属性值 说明
none 不论动画播放次数,结束后返回原本样式 ( 默认值 )。
forwards 动画结束后,保持在 100% 状态。
backwards 动画结束后,保持在 0% 状态 ( 大部分浏览器等同 none 的效果 )。
both 依据动画的次数或播放方向,保持在第一个影格或最后一个影格状态 ( 很实用 )。

下方范例会展示这四种动画播放前后模式的差异,可观察结束后的位置和颜色。

线上展示:https://codepen.io/oxxo/pen/WNVmEJJ

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<h3>none</h3><div></div>
<h3>forwards</h3><div></div>
<h3>backwards</h3><div></div>
<h3>both</h3><div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {border: 2px solid #000;}
  h2:hover~div {
    animation-name: oxxo;
    animation-duration: 2s;
    animation-iteration-count: 3;
    animation-direction: alternate;
  }
  h3 {
    margin:5px;
    width: 100px;
    height: 30px;
    float:left;
    clear: both;
    text-align: right;
  }
  div {
    float:left;
    width: 30px;
    height: 30px;
    margin:5px;
    background: green;
  }
  div:nth-of-type(1) {animation-fill-mode: none;}
  div:nth-of-type(2) {animation-fill-mode: forwards;}
  div:nth-of-type(3) {animation-fill-mode: backwards;}
  div:nth-of-type(4) {animation-fill-mode: both;}

  @keyframes oxxo{
    0% {
      background: red;
      margin-left: 5px;
    }
    100% {
      background: cyan;
      margin-left: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - animation-fill-mode 动画播放前后模式

animation-play-state 动画播放或暂停

animation-play-state 表示“动画播放或暂停”的样式属性,可以透过这个样式属性控制动画播放或暂停,没有继承特性,有下列两种属性值:

属性值 说明
running 动画运行 ( 默认值 ) 。
paused 动画暂停。

下方范例执行后,当鼠标移动到上方的按钮,下方的动画就会暂停,鼠标移开之后动画就会继续。

线上展示:https://codepen.io/oxxo/pen/abeMyKo

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {
    width: 300px;
    border: 2px solid #000;
  }
  h2:hover~div {
    animation-play-state: paused; /* 滑鼠移動上去時,暫停動畫 */
  }
  div {
    width: 50px;
    height: 50px;
    margin:5px;
    animation-name: oxxo;
    animation-duration: 2s;
    animation-iteration-count: infinite; /* 動畫無限播放 */
    background: red;
  }

  @keyframes oxxo{
    50% {
      background: cyan;
      margin-left: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - animation-play-state 动画播放或暂停

animation-composition 动画合成设定

animation-composition 表示“动画与样式属性的合成关系”,这个样式属性可以*设定套用动画的选择器,与动画中出现相同样式属性时,针对该样式属性数值进行取代或叠加的方式,且只针对属性值为“数值”的样式属性才有作用,具有下列的属性值:

属性值 说明
replace 取代原本的属性值。
add 叠加原本的样式属性值。
accumulate 累加原本的样式属性值。

属性值的 add 和 accumulate 虽然都是叠加效果,但 add 表示在原本的属性值的状态下进行叠加,accumulate 则是只会在动画中属性值的位置进行叠加,这种特性只有在“多重属性值”才会有明显的差异,下方范例将几个不同的 h2 分别套用 transform: translateX(30px) rotate(45deg) 样式,a、b、c 是套用动画组,a1、a2、a3 是实际套用样式的非动画效果,最后重叠再一起会呈现紫色效果,从中可以看出 add 和 accumulate 对于最终结果影响的差异。

线上展示:https://codepen.io/oxxo/pen/ByNxMVo

<!-- HTML 程式碼-->
<h4>原始位置</h4>
<h4 class="a">replace</h4>
<h4 class="a1">replace 對照</h4>
<h4 class="b">add</h4>
<h4 class="b1">add 對照</h4>
<h4 class="c">accumulate</h4>
<h4 class="c1">accumulate 對照</h4>

<!-- CSS 程式碼 -->
<style>
  h4 {
    position: fixed;
    width: 100px;
    height: 15px;
    top: 50px;
    left: ㄅ0px;
    border: 1px solid #000;
    font-size: 10px;
    background: #00f4;
  }
  .a, .b, .c {
    background: #f005;
    transform: translateX(30px) rotate(45deg);
    animation: oxxo 1s forwards linear;
  }
  .a {animation-composition: replace;}
  .a1 {transform: translateX(200px);}   /* .a 最後位置 */

  .b {animation-composition: add;}
  .b1 {transform: translateX(30px) rotate(45deg) translateX(200px);}   /* .b 最後位置,注意順序 */

  .c {animation-composition: accumulate;}
  .c1 {transform: translateX(30px) translateX(200px) rotate(45deg);}   /* .c 最後位置,注意順序 */

  @keyframes oxxo {
    0% {transform: translateX(100px);}
    100% {transform: translateX(200px);}
  }
</style>

CSS 教学 - 动画 animation - animation-composition 动画合成设定

这种合成设定也可用于“多重动画”,下方的例子同时有两组动画作用于 transform,如果使用默认设定值,transform 会被最后一组动画覆盖而只呈现旋转效果,但套用 add 或 accumulate 之后,就会使用不同的叠加方式进行叠加。

线上展示:https://codepen.io/oxxo/pen/yyNjZEX

<!-- HTML 程式碼-->
<h4>原始位置</h4>
<h4 class="a">replace</h4>
<h4 class="a1">replace 對照</h4>
<h4 class="b">add</h4>
<h4 class="b1">add 對照</h4>
<h4 class="c">accumulate</h4>
<h4 class="c1">accumulate 對照</h4>

<!-- CSS 程式碼 -->
<style>
  h4 {
    position: fixed;
    width: 100px;
    height: 15px;
    top: 50px;
    left: 10px;
    border: 1px solid #000;
    font-size: 10px;
    background: #00f4;
  }
  .a, .b, .c {
    background: #f005;
    transform: translateX(30px) rotate(45deg);
    animation: oxxo 1s forwards linear, oxxo2 1s forwards linear; /* 套用雙重動畫 */
  }
  .a {animation-composition: replace;}
  .a1 {transform: rotate(135deg);}     /* .a 最後位置 */

  .b {animation-composition: add;}
  .b1 {transform: translateX(30px) rotate(45deg) translateX(200px) rotate(135deg);}  /* .b 最後位置,注意順序 */

  .c {animation-composition: accumulate;}
  .c1 {transform: translateX(30px) translateX(200px) rotate(45deg) rotate(135deg);}  /* .c 最後位置,注意順序 */

  @keyframes oxxo {
    0% {transform: translateX(100px);}
    100% {transform: translateX(200px);}
  }
  @keyframes oxxo2 {
    0% {transform: rotate(45deg);}
    100% {transform: rotate(135deg);}
  }
</style>

CSS 教学 - 动画 animation - animation-composition 动画合成设定 ( 多重动画 )

animation 动画缩写样式

元素套用动画效果时,为了避免 CSS 程序码过于冗长,有时会使用缩写的 animation 样式属性,使用时只需要将动画的个别样式写在后方,除了动画持续时间和延迟时间有顺序 ( 持续时间, 延迟时间 ),其他关键字排列没有顺序之分,但仍需注意“动画名称”不要与特殊关键字相同,下方所列出的写法都是合乎规规则的写法:

注意,animation-composition 不属于缩写的属性范围,要独立撰写。

div {
  animation: 1s oxxo;                       /* 持續時間 1s */
  animation: oxxo 1s;                       /* 持續時間 1s */
  animation: 1s 0.5s oxxo;                  /* 持續時間 1s,延遲時間 0.5s */
  animation: 1s ease-in infinite oxxo;      /* 持續時間 1s,使用 ease-in,無限重複  */
  animation: 1s oxxo 0.5s ease-in infinite; /* 持續時間 1s,延遲時間 0.,5s,使用 ease-in,無限重複 */
  animation: 1s ease-in 3 backwards oxxo;   /* 持續時間 1s,使用 ease-in,重複三次,backwards 模式 */
}

下方的范例使用 animation 让四个 div 呈现不同的动画效果。

线上展示:https://codepen.io/oxxo/pen/KKOEvex

<!-- HTML 程式碼-->
<h2>滑鼠移動到我上方看效果</h2>
<h3>animation: 2s oxxo</h3><div></div>
<h3>animation: 2s 0.5s ease-in oxxo</h3><div></div>
<h3>animation: 4 1s oxxo ease-in</h3><div></div>
<h3>animation: 1s 1s oxxo 3 both ease-out</h3><div></div>

<!-- CSS 程式碼 -->
<style>
  h2 {border: 2px solid #000;}
  h3 {
    margin:5px;
    height: 30px;
  }
  div {
    width: 30px;
    height: 30px;
    margin:5px;
    background: red;
  }
  h2:hover~div:nth-of-type(1) {animation: 2s oxxo;}
  h2:hover~div:nth-of-type(2) {animation: 2s 0.5s ease-in oxxo;}
  h2:hover~div:nth-of-type(3) {animation: 4 1s oxxo ease-in;}
  h2:hover~div:nth-of-type(4) {animation: 1s 1s oxxo 3 both ease-out;}

  @keyframes oxxo{
    50% {
      background: cyan;
      margin-left: 200px;
    }
  }
</style>

CSS 教学 - 动画 animation - animation 动画缩写样式

小结

animation 是一个非常好用的 CSS 样式属性,随着浏览器的进步,目前几乎所有的浏览器都支援 CSS 动画功能,只要熟悉相关的语法,相信一定可以做出非常酷炫的动画效果。

阅读更多:CSS animation 触发事件、自订 CSS 动画路径 offset、CSS 多重动画的权重与顺序

意见反馈

微信二维码 微信扫码关注 抖音二维码 抖音扫码关注