第十五天:动画与伪元素 — 呼吸灯、数据跳动与"隐形元素"
蜗牛往上爬 · 前端工业可视化学习笔记 第 15 篇
前两天学了 transition(触发式过渡),今天学两个更强大的能力:@keyframes动画(自动播放)和::before/::after伪元素(不写 HTML 也能插入元素)。两者结合,就能做出大屏上的呼吸灯边框、数据跳动、旋转加载——这些正是工业大屏的视觉灵魂。
目录
一、@keyframes:定义动画帧
/* 定义动画:名称 + 关键帧 */
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.1); opacity: 0.7; }
100% { transform: scale(1); opacity: 1; }
}
/* 使用动画 */
.element {
animation: pulse 2s ease-in-out infinite;
}
from / to 是 0% / 100% 的简写:
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
二、animation 属性速查
.element {
/* 动画名称(必填,对应 @keyframes 的名字) */
animation-name: pulse;
/* 时长(必填) */
animation-duration: 2s;
/* 缓动 */
animation-timing-function: ease-in-out;
/* 延迟 */
animation-delay: 0s;
/* 播放次数:1 / 2 / infinite(无限循环) */
animation-iteration-count: infinite;
/* 方向:normal / reverse / alternate(往返) */
animation-direction: normal;
/* 填充模式:动画结束后保持哪个状态 */
animation-fill-mode: none;
/* 简写(推荐) */
animation: pulse 2s ease-in-out infinite;
}
transition vs animation 一句话区分
三、常用动画模板
/* 呼吸灯 */
@keyframes breathe {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
/* 数据跳动 */
@keyframes countUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
/* 旋转 */
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* 闪烁 */
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
/* 边框呼吸 */
@keyframes borderBreathe {
0%, 100% { border-color: rgba(0,255,136,0.1); }
50% { border-color: rgba(0,255,136,0.6); }
}
四、伪元素:::before 和 ::after
伪元素可以在不修改 HTML 的情况下,给元素"插入"额外内容。
/* ::before:在元素内容前面插入 */
.card::before {
content: "★"; /* 必须写 content,即使空字符串也要写 "" */
color: gold;
margin-right: 8px;
}
/* ::after:在元素内容后面插入 */
.card::after {
content: "";
display: block;
width: 100%;
height: 2px;
background: #1a3a5c;
margin-top: 12px;
}
伪元素 vs 真实 DOM
🧠 黄金法则:装饰用伪元素,功能用真实 DOM。呼吸灯边框、角标装饰、分割线——这些纯视觉的元素用伪元素最合适,不污染 HTML 结构。
五、伪元素 + 动画:呼吸灯边框
这是大屏上最经典的效果——面板边框像呼吸一样明暗变化。
.panel {
position: relative; /* 为伪元素提供定位上下文 */
}
.panel::before {
content: "";
position: absolute;
top: -2px; left: -2px;
right: -2px; bottom: -2px;
border-radius: inherit;
border: 2px solid transparent;
animation: borderBreathe 2s ease-in-out infinite;
pointer-events: none; /* 不阻挡鼠标事件 */
}
@keyframes borderBreathe {
0%, 100% {
border-color: rgba(0, 255, 136, 0.1);
box-shadow: 0 0 8px rgba(0, 255, 136, 0);
}
50% {
border-color: rgba(0, 255, 136, 0.6);
box-shadow: 0 0 20px rgba(0, 255, 136, 0.3);
}
}
六、动手练:大屏面板呼吸灯 + 数据跳动
做一个模拟大屏面板,含呼吸灯边框、数据跳动、旋转加载。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>大屏面板动效</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: "Microsoft YaHei", sans-serif;
background: #0a1628;
color: white;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
gap: 24px;
padding: 20px;
flex-wrap: wrap;
}
.panel {
width: 280px;
padding: 24px;
background: rgba(10, 20, 40, 0.9);
border: 1px solid rgba(26, 92, 138, 0.4);
border-radius: 8px;
position: relative;
overflow: hidden;
}
.panel h3 { font-size: 14px; color: #8ecae6; margin-bottom: 16px; padding-bottom: 8px; border-bottom: 1px solid rgba(26,92,138,0.3); }
.panel .value {
font-size: 42px; font-weight: bold; text-align: center;
color: #00ff88;
animation: countUp 0.6s ease-out;
}
.panel .sub { text-align: center; color: #8ecae6; font-size: 13px; margin-top: 4px; }
@keyframes countUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
/* 呼吸灯边框 */
.panel.breathe::before {
content: "";
position: absolute;
top: -1px; left: -1px; right: -1px; bottom: -1px;
border-radius: 9px;
border: 2px solid transparent;
animation: borderBreathe 2s ease-in-out infinite;
pointer-events: none;
}
@keyframes borderBreathe {
0%, 100% { border-color: rgba(0,255,136,0.1); box-shadow: 0 0 8px rgba(0,255,136,0); }
50% { border-color: rgba(0,255,136,0.6); box-shadow: 0 0 20px rgba(0,255,136,0.3); }
}
/* 告警:红色呼吸灯 */
.panel.alert { border-color: rgba(214,40,40,0.4); }
.panel.alert .value { color: #ff6b6b; }
.panel.alert::before {
content: "";
position: absolute;
top: -1px; left: -1px; right: -1px; bottom: -1px;
border-radius: 9px;
border: 2px solid transparent;
animation: alertBreathe 1s ease-in-out infinite;
pointer-events: none;
}
@keyframes alertBreathe {
0%, 100% { border-color: rgba(255,107,107,0.1); box-shadow: 0 0 8px rgba(255,107,107,0); }
50% { border-color: rgba(255,107,107,0.8); box-shadow: 0 0 24px rgba(255,107,107,0.4); }
}
/* 旋转加载 */
.panel.loading::after {
content: "";
display: block;
width: 24px; height: 24px;
margin: 12px auto 0;
border: 3px solid rgba(26,92,138,0.3);
border-top-color: #8ecae6;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="panel breathe">
<h3>设备总数</h3>
<div class="value">48</div>
<div class="sub">较昨日 +2</div>
</div>
<div class="panel alert">
<h3>告警中</h3>
<div class="value">3</div>
<div class="sub">需立即处理</div>
</div>
<div class="panel loading">
<h3>数据加载中</h3>
<div class="value">--</div>
<div class="sub">正在获取实时数据</div>
</div>
</body>
</html>
动效分析:
练习:照敲 → 把呼吸灯周期从 2s 改成 1s → 把告警颜色从红色改成橙色 → 给旋转面板加一个呼吸边框。
七、常见坑点与最佳实践
坑 1:伪元素没写 content
/* ❌ 伪元素必须写 content,否则不显示 */
.box::before {
width: 100px; height: 100px;
background: red;
}
/* ✅ 即使不需要文字,也要写 content: "" */
.box::before {
content: "";
width: 100px; height: 100px;
background: red;
}
坑 2:动画不播放
/* ❌ 只写了 animation-name,没写 duration */
.box { animation: pulse; }
/* ✅ 必须同时写 name 和 duration */
.box { animation: pulse 2s; }
坑 3:伪元素盖住了内容
/* 伪元素绝对定位可能盖住面板内容 */
.panel::before {
position: absolute;
z-index: 1; /* 如果在内容之上,会挡住 */
}
/* 解决:加 pointer-events: none 或降低 z-index */
.panel::before {
pointer-events: none;
z-index: -1;
}
最佳实践清单
[x] 伪元素
content必须写,即使空字符串[x] 动画必须同时写
animation-name和animation-duration[x] 装饰用伪元素,功能用真实 DOM
[x] 呼吸灯用
::before+@keyframes,不污染 HTML[x] 伪元素绝对定位时加
pointer-events: none
八、这些知识在工业可视化里用在哪?
大屏 = 呼吸灯 + 数据跳动 + 旋转加载
/* 正常面板:绿色呼吸 */
.panel { /* ::before + borderBreathe */ }
/* 告警面板:红色快速呼吸 */
.panel.alert { /* ::before + alertBreathe @ 1s */ }
/* 数据加载:旋转 */
.panel.loading { /* ::after + spin */ }
/* 数字更新:跳动 */
.value { animation: countUp 0.6s ease-out; }
这些动效就是大屏的"视觉灵魂"——没有它们,大屏就是一堆静态数字;有了它们,大屏才有了"实时监控"的感觉。
九、自测挑战 10 题
@keyframes的from和to分别等价于0%和100%吗?animation: pulse 2s ease-in-out infinite;各值分别什么含义?animation和transition的核心区别?写出一个呼吸灯动画(opacity 在 1 和 0.3 之间变化)。
::before和::after的content必须写吗?伪元素和真实 DOM 有什么区别?为什么用伪元素?
写出一个旋转 360 度的动画。
把呼吸灯动画从
ease-in-out改成linear,观察效果。给面板加一个"闪烁"动画(
animation而非transition)。用 F12 把动画的
animation-play-state改成paused,暂停动画。
加分挑战:用伪元素 + 动画做一个"脉冲圆点"(一个圆点放大再缩小,循环播放)。
十、总结
今天学完,你应该能做到:
[x] 使用
@keyframes定义关键帧动画[x] 使用
animation简写并理解各参数含义[x] 区分
transition(触发式)和animation(自动播放)[x] 使用
::before和::after伪元素[x] 做出呼吸灯边框、数据跳动、旋转加载动效
[x] 理解"装饰用伪元素,功能用真实 DOM"的原则
一句话带走:@keyframes 让动画自动播放,::before/::after 让装饰不污染 HTML。两者结合,就是大屏的视觉灵魂——呼吸灯、数据跳动、旋转加载。
📎 本文为"蜗牛往上爬"学习博客系列 · 阶段 0(HTML+CSS)第 5 周 · 第 3 篇