大家好,我是Java1234_小锋老师,最近更新 《一天学会 CSS3 视频教程(无废话版) 》专辑,感谢大家支持。

本课程主要介绍和讲解 CSS3简介,CSS3 入门,CSS3 选择器与优先级,CSS3 单位、颜色、字体 与文本,CSS3 盒模型 与基础样式,CSS3 布局基础,CSS3 布局进阶,CSS3 视觉 增强,CSS3 变换与动画,CSS3 函数与变量,CSS3 响应式 与移动端适配,CSS3 工程化与最佳实践。
视频教程+课件+源码 打包下载 :
链接:https://pan.baidu.com/s/1o-zRfndo1HHrS%5FuFroOiCw?pwd=1234
提取码:1234
CSS3(在第二阶段目录里)
CSS3 变换与动画
CSS 变换(transform)改变元素的视觉形态,但不改变文档流,元素原本占据的空间仍在。过渡(transition)和动画(animation)则让这些变换或其他可动画属性随时间变化。
1. 2D 变换
使用 transform,多个函数可组合,顺序不同结果不同。
1.box { 2 transform: translate(20px, 10px); /* 平移 */ 3 transform: rotate(45deg); /* 旋转 */ 4 transform: scale(1.5, 0.8); /* 缩放 */ 5 transform: skew(10deg, 5deg); /* 倾斜 */ 6 7 /* 组合 */ 8 transform: translate(20px, 0) rotate(30deg) scale(1.2) skew(5deg); 9} 10
常见函数:
| 函数 | 作用 | 示例 |
|---|---|---|
| translate(x, y) | 平移,百分比相对自身尺寸 | translate(50%, 0) |
| translateX() / translateY() | 单轴平移 | translateY(-10px) |
| rotate(deg) | 顺时针旋转 | rotate(45deg) |
| scale(x, y) | 缩放,1 为原始大小 | scale(1.2, 0.8) |
| scaleX() / scaleY() | 单轴缩放 | scaleX(1.5) |
| skew(ax, ay) | 倾斜,单位 deg | skew(10deg, 5deg) |
| skewX() / skewY() | 单轴倾斜 | skewX(15deg) |
2. 3D 变换
3D 变换让元素在三维空间中旋转、平移。常用函数:
1.card { 2 transform: rotateX(30deg) rotateY(45deg) rotateZ(10deg) translateZ(50px); 3} 4
rotateX(deg):绕 X 轴旋转。rotateY(deg):绕 Y 轴旋转。rotateZ(deg):绕 Z 轴旋转,等价于 2Drotate()。translateZ(z):沿 Z 轴平移,正值朝观察者方向。perspective:透视,决定 3D 效果的强弱。
perspective 通常设在父元素上:
1.scene { 2 perspective: 800px; /* 数值越小,透视越强 */ 3 perspective-origin: 50% 50%; 4} 5 6.card { 7 transform: rotateY(45deg) translateZ(50px); 8 transform-style: preserve-3d; /* 子元素保留 3D 空间 */ 9 backface-visibility: hidden; /* 隐藏背面 */ 10} 11
也可以直接在 transform 中使用 perspective():
1.box { 2 transform: perspective(800px) rotateY(45deg); 3} 4
注意:perspective() 通常放在变换函数最前面。
3. 变形原点:transform-origin
transform-origin 决定旋转、缩放、倾斜围绕哪个点进行。默认是元素中心:
1.box { 2 transform-origin: 50% 50% 0; /* 默认值 */ 3} 4 5.box-left { 6 transform-origin: left top; 7} 8 9.box-custom { 10 transform-origin: 10px 20px 0; 11} 12
它主要影响 rotate、scale、skew 等,对单纯的 translate 影响不明显。
4. 过渡:transition
transition 用于元素从一种状态平滑过渡到另一种状态,通常由 :hover、类名切换等触发。
1.button { 2 background: #3498db; 3 transform: translateY(0); 4 transition: transform 0.3s ease, background 0.3s linear; 5} 6 7.button:hover { 8 transform: translateY(-3px); 9 background: #2980b9; 10} 11
简写顺序:
1transition: 属性 持续时间 缓动函数 延迟; 2transition: transform 0.3s ease-in-out 0.1s; 3
多个属性用逗号分隔:
1transition: transform 0.3s ease, opacity 0.5s linear; 2
transition 需要状态变化才能触发,不能自动循环。
5. 动画:animation 与 @keyframes
@keyframes 定义关键帧,animation 把动画应用到元素上。
1@keyframes pulse { 2 0% { 3 transform: scale(1); 4 opacity: 1; 5 } 6 50% { 7 transform: scale(1.1); 8 opacity: 0.7; 9 } 10 100% { 11 transform: scale(1); 12 opacity: 1; 13 } 14} 15 16.loading { 17 animation: pulse 1.5s ease-in-out 0s infinite alternate both running; 18} 19
更推荐分开写,清晰且不易混淆:
1.loading { 2 animation-name: pulse; 3 animation-duration: 1.5s; 4 animation-timing-function: ease-in-out; 5 animation-delay: 0s; 6 animation-iteration-count: infinite; 7 animation-direction: alternate; 8 animation-fill-mode: both; 9 animation-play-state: running; 10} 11
6. 动画属性详解
| 属性 | 作用 | 常用值 |
|---|---|---|
| animation-duration | 动画持续时间 | 2s、500ms |
| animation-timing-function | 缓动函数 | ease、linear、ease-in、ease-out、ease-in-out、cubic-bezier()、steps() |
| animation-delay | 延迟多久开始 | 1s、-0.5s |
| animation-iteration-count | 播放次数 | 3、infinite |
| animation-direction | 播放方向 | normal、reverse、alternate、alternate-reverse |
| animation-fill-mode | 动画前后样式保留 | none、forwards、backwards、both |
| animation-play-state | 播放或暂停 | running、paused |
示例:鼠标悬停暂停动画:
1.loading:hover { 2 animation-play-state: paused; 3} 4
animation 简写中,第一个时间值是 duration,第二个时间值是 delay:
1animation: pulse 2s ease-in 1s infinite alternate both running; 2/* 名称 时长 缓动 延迟 次数 方向 填充 状态 */ 3
7. 性能优化
动画性能关键:尽量只动画 transform 和 opacity,因为它们通常只触发合成,不触发布局和重绘。
1.box { 2 will-change: transform, opacity; 3 transform: translateZ(0); 4 opacity: 1; 5} 6
建议:
- 用
transform: translate()代替top/left。 - 用
transform: scale()代替直接改width/height。 - 用
opacity做淡入淡出,避免频繁改visibility或display。 will-change: transform, opacity;提前告诉浏览器优化,但不要滥用,动画结束后最好移除。- 必要时用
translateZ(0)或translate3d(0,0,0)开启 GPU 合成层,但层太多会占内存。 - 避免动画
width、height、margin、padding、border等触发布局的属性。 - 尊重用户减少动画偏好:
1@media (prefers-reduced-motion: reduce) { 2 * { 3 animation-duration: 0.01ms !important; 4 transition-duration: 0.01ms !important; 5 } 6} 7
8. 综合实例
1<!DOCTYPE html> 2<html lang="zh-CN"> 3<head> 4<meta charset="UTF-8"> 5<meta name="viewport" content="width=device-width, initial-scale=1"> 6<title>CSS3 变换与动画 · 综合实例</title> 7<style> 8/* ===================== 全局与主题 ===================== */ 9:root{ 10 --bg: #0f1226; 11 --panel: #181c35; 12 --line: #2a3057; 13 --text: #e8ecf7; 14 --muted: #8f9bc4; 15 --blue: #3b6bff; 16 --pink: #ff5da2; 17 --green: #22c55e; 18} 19 20*,*::before,*::after{ box-sizing:border-box; } 21 22body{ 23 margin:0; 24 padding:40px 16px 96px; 25 font-family:system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif; 26 line-height:1.7; 27 color:var(--text); 28 background:radial-gradient(1200px 600px at 50% -10%, #1d2450 0%, var(--bg) 60%); 29} 30 31header{ max-width:1040px; margin:0 auto 40px; text-align:center; } 32header h1{ font-size:clamp(22px,3vw,32px); margin:0 0 8px; } 33header p{ color:var(--muted); margin:0; } 34 35section{ 36 max-width:1040px; 37 margin:0 auto 40px; 38 padding:24px 28px 32px; 39 background:var(--panel); 40 border:1px solid var(--line); 41 border-radius:18px; 42} 43 44section h2{ 45 font-size:18px; 46 margin:0 0 4px; 47 display:flex; 48 align-items:center; 49 gap:10px; 50} 51section h2::before{ 52 content:""; 53 width:8px; height:8px; 54 border-radius:50%; 55 background:var(--blue); 56 box-shadow:0 0 12px var(--blue); 57 flex:none; 58} 59.hint{ color:var(--muted); font-size:14px; margin:0 0 20px; } 60.hint code{ 61 background:#20264a; 62 border-radius:4px; 63 padding:1px 6px; 64 font-size:12.5px; 65} 66 67.stage{ 68 display:flex; 69 flex-wrap:wrap; 70 align-items:center; 71 gap:28px; 72} 73 74/* ===================== 1. 2D 变换 ===================== */ 75.demo{ 76 width:120px; height:120px; 77 display:grid; place-items:center; 78 border-radius:14px; 79 background:linear-gradient(135deg,#3b6bff,#7b3bff); 80 color:#fff; font-size:13px; font-weight:600; 81 cursor:pointer; 82 user-select:none; 83 box-shadow:0 6px 18px rgba(0,0,0,.35); 84 /* 这里的 transition 让变形平滑 —— 见第 4 章 */ 85 transition:transform .45s cubic-bezier(.2,.8,.3,1), 86 box-shadow .45s ease; 87} 88.demo:hover{ box-shadow:0 14px 30px rgba(59,107,255,.35); } 89 90.t-translate:hover{ transform:translate(28px,-14px); } 91.t-rotate:hover { transform:rotate(30deg); } 92.t-scale:hover { transform:scale(1.25); } 93.t-skew:hover { transform:skew(12deg,6deg); } 94.t-combo:hover { transform:translate(12px,-12px) rotate(15deg) scale(1.1); } 95 96/* ===================== 2. 3D 变换 ===================== */ 97.scene{ 98 width:200px; height:200px; 99 flex:none; 100 perspective:700px; /* 透视:数值越小,立体感越强 */ 101 perspective-origin:50% 50%; 102} 103 104.cube{ 105 position:relative; 106 width:120px; height:120px; 107 margin:40px; 108 transform-style:preserve-3d; /* 子元素保留 3D 空间 */ 109 animation:cubeSpin 12s linear infinite; 110} 111@keyframes cubeSpin{ 112 from{ transform:rotateX(-20deg) rotateY(0deg); } 113 to { transform:rotateX(-20deg) rotateY(360deg); } 114} 115.cube .face{ 116 position:absolute; 117 inset:0; 118 display:grid; place-items:center; 119 font-size:13px; 120 border:2px solid rgba(255,255,255,.55); 121 background:rgba(59,107,255,.5); 122} 123.face.front { transform:translateZ(60px); } 124.face.back { transform:rotateY(180deg) translateZ(60px); background:rgba(255,93,162,.5); } 125.face.right { transform:rotateY(90deg) translateZ(60px); background:rgba(34,197,94,.5); } 126.face.left { transform:rotateY(-90deg) translateZ(60px); background:rgba(250,204,21,.5); } 127.face.top { transform:rotateX(90deg) translateZ(60px); background:rgba(168,85,247,.5); } 128.face.bottom{ transform:rotateX(-90deg) translateZ(60px); background:rgba(20,184,166,.5); } 129 130.tilt-wrap{ 131 width:200px; height:200px; 132 flex:none; 133 perspective:700px; 134 display:grid; place-items:center; 135} 136.tilt-card{ 137 width:130px; height:170px; 138 display:grid; place-items:center; 139 padding:10px; 140 text-align:center; 141 font-size:13px; 142 border-radius:14px; 143 background:linear-gradient(160deg,#2c3a72,#1b2244); 144 border:1px solid #3a4680; 145 cursor:pointer; 146 transition:transform .5s cubic-bezier(.2,.8,.3,1), box-shadow .5s ease; 147} 148.tilt-card:hover{ 149 transform:rotateY(28deg) rotateX(-12deg) translateZ(45px); 150 box-shadow:-18px 22px 44px rgba(0,0,0,.55); 151} 152 153/* ===================== 3. 变形原点 ===================== */ 154.stage-origin{ padding:20px 10px; } 155 156.origin-box{ 157 width:150px; height:100px; 158 display:grid; place-items:center; 159 font-size:12px; 160 border-radius:12px; 161 background:linear-gradient(135deg,#3b6bff,#7b3bff); 162 border:1px dashed rgba(255,255,255,.6); 163 animation:sway 2s ease-in-out infinite alternate; 164} 165@keyframes sway{ 166 from{ transform:rotate(0deg); } 167 to { transform:rotate(40deg); } 168} 169 170.o-center { transform-origin:50% 50%; } /* 默认:中心 */ 171.o-top-left { transform-origin:0 0; } 172.o-bottom-right{ transform-origin:100% 100%; } 173 174/* ===================== 4. 过渡 transition ===================== */ 175.tr-card{ 176 width:250px; 177 padding:20px 22px; 178 border-radius:16px; 179 background:#222a4d; 180 border:1px solid #333d70; 181 cursor:pointer; 182 transition:transform .45s cubic-bezier(.2,.8,.3,1), 183 box-shadow .45s ease, 184 background-color .45s ease; 185} 186.tr-card:hover{ 187 transform:translateY(-10px) scale(1.04); 188 box-shadow:0 20px 44px rgba(59,107,255,.45); 189 background-color:#2c3a72; 190} 191.tr-card h4{ margin:0 0 6px; font-size:15px; } 192.tr-card p { margin:0; font-size:13px; color:var(--muted); } 193 194.dots{ display:flex; gap:14px; padding:20px 6px; } 195.dot{ 196 width:20px; height:20px; 197 border-radius:50%; 198 background:var(--blue); 199 transition:transform .4s cubic-bezier(.2,.8,.3,1), 200 background-color .4s ease; 201} 202.dots:hover .dot{ transform:translateY(-18px); background-color:var(--pink); } 203.dots:hover .dot:nth-child(2){ transition-delay:.08s; } 204.dots:hover .dot:nth-child(3){ transition-delay:.16s; } 205.dots:hover .dot:nth-child(4){ transition-delay:.24s; } 206 207/* ===================== 5. animation 与 @keyframes ===================== */ 208.pulse{ 209 width:90px; height:90px; 210 border-radius:50%; 211 background:var(--blue); 212 animation:pulse 1.8s ease-out infinite; 213} 214@keyframes pulse{ 215 0% { transform:scale(1); box-shadow:0 0 0 0 rgba(59,107,255,.75); } 216 70% { transform:scale(1.06); box-shadow:0 0 0 22px rgba(59,107,255,0); } 217 100%{ transform:scale(1); box-shadow:0 0 0 0 rgba(59,107,255,0); } 218} 219 220.bounce{ 221 width:70px; height:70px; 222 border-radius:16px; 223 background:var(--pink); 224 animation:bounce 1s ease-in-out infinite; 225} 226@keyframes bounce{ 227 0%,100%{ transform:translateY(0); } 228 50% { transform:translateY(-34px); } 229} 230 231.spin-ring{ 232 width:80px; height:80px; 233 border-radius:50%; 234 border:6px solid #2a3057; 235 border-top-color:var(--green); 236 animation:spin 1.1s linear infinite; 237} 238@keyframes spin{ 239 to{ transform:rotate(360deg); } 240} 241 242/* ===================== 6. 动画属性控制器 ===================== */ 243.controls{ 244 display:grid; 245 grid-template-columns:repeat(auto-fit,minmax(210px,1fr)); 246 gap:16px 24px; 247 margin-bottom:24px; 248} 249.controls label{ 250 display:flex; 251 flex-direction:column; 252 gap:6px; 253 font-size:13px; 254 color:var(--muted); 255} 256.controls label > span{ 257 display:flex; 258 justify-content:space-between; 259 align-items:baseline; 260 gap:8px; 261} 262.controls output{ color:var(--text); font-weight:600; } 263 264.controls select{ 265 width:100%; 266 padding:7px 9px; 267 font-size:13px; 268 color:var(--text); 269 background:#20264a; 270 border:1px solid #333d70; 271 border-radius:8px; 272} 273.controls input[type="range"]{ 274 width:100%; 275 accent-color:var(--blue); 276} 277 278.ctrl-track{ 279 position:relative; 280 height:120px; 281 border-radius:14px; 282 border:1px solid var(--line); 283 background: 284 repeating-linear-gradient(90deg,#20264a 0 24px,#242b52 24px 48px); 285 overflow:hidden; 286} 287 288.ctrl-box{ 289 position:absolute; 290 left:16px; top:24px; 291 width:72px; height:72px; 292 display:grid; place-items:center; 293 font-size:13px; font-weight:600; color:#fff; 294 border-radius:14px; 295 background-color:#3b6bff; 296 /* 以下属性都会由 JS 动态覆盖 */ 297 animation-name:demoMove; 298 animation-duration:2s; 299 animation-timing-function:ease-in-out; 300 animation-delay:0s; 301 animation-iteration-count:infinite; 302 animation-direction:normal; 303 animation-fill-mode:none; 304 animation-play-state:running; 305 will-change:transform, opacity; /* 见第 7 章 */ 306} 307@keyframes demoMove{ 308 0% { transform:translateX(0) scale(1); opacity:1; background-color:#3b6bff; } 309 50% { transform:translateX(100px) scale(1.15); opacity:.55; background-color:#ff5da2; } 310 100%{ transform:translateX(200px) scale(1); opacity:1; background-color:#22c55e; } 311} 312 313.btns{ display:flex; flex-wrap:wrap; gap:12px; margin-top:20px; } 314button{ 315 padding:8px 18px; 316 font-size:13px; 317 color:var(--text); 318 background:#20264a; 319 border:1px solid #3a4680; 320 border-radius:10px; 321 cursor:pointer; 322 transition:background-color .2s ease, transform .2s ease; 323} 324button:hover { background:#2c3a72; transform:translateY(-2px); } 325button:active{ transform:translateY(0); } 326 327/* ===================== 7. 性能优化对比 ===================== */ 328.perf-item{ margin-bottom:20px; } 329.perf-item .name{ font-size:13px; color:var(--muted); margin-bottom:8px; } 330.perf-track{ 331 position:relative; 332 height:70px; 333 border-radius:12px; 334 border:1px solid var(--line); 335 background:#20264a; 336 overflow:hidden; 337} 338.perf-box{ 339 position:absolute; 340 top:11px; left:12px; 341 width:48px; height:48px; 342 border-radius:12px; 343} 344 345/* 推荐:只动 transform / opacity,走合成层 */ 346.perf-box.good{ 347 background:linear-gradient(135deg,#22c55e,#14b8a6); 348 will-change:transform, opacity; 349 transform:translateZ(0); /* 提示开启 GPU 合成层 */ 350 animation:goodMove 2s ease-in-out infinite; 351} 352@keyframes goodMove{ 353 0%,100%{ transform:translateX(0) translateZ(0); opacity:1; } 354 50% { transform:translateX(180px) translateZ(0); opacity:.45; } 355} 356 357/* 反例:动 left / width,会触发布局与重绘 */ 358.perf-box.bad{ 359 background:linear-gradient(135deg,#ff5da2,#ff8a3d); 360 animation:badMove 2s ease-in-out infinite; 361} 362@keyframes badMove{ 363 0%,100%{ left:12px; width:48px; } 364 50% { left:180px; width:88px; } 365} 366 367/* ===================== 无障碍:尊重减少动画偏好 ===================== */ 368@media (prefers-reduced-motion: reduce){ 369 *,*::before,*::after{ 370 animation-duration:.01ms !important; 371 animation-iteration-count:1 !important; 372 transition-duration:.01ms !important; 373 } 374} 375</style> 376</head> 377<body> 378 379<header> 380 <h1>CSS3 变换与动画 · 综合实例</h1> 381 <p>2D 变换 · 3D 变换 · 变形原点 · 过渡 · 关键帧动画 · 动画属性 · 性能优化</p> 382</header> 383 384<!-- ==================== 1. 2D 变换 ==================== --> 385<section> 386 <h2>1. 2D 变换:translate / rotate / scale / skew</h2> 387 <p class="hint">把鼠标移到方块上看效果。多个函数可以连写,<strong>顺序不同结果不同</strong>。</p> 388 <div class="stage"> 389 <div class="demo t-translate">translate</div> 390 <div class="demo t-rotate">rotate</div> 391 <div class="demo t-scale">scale</div> 392 <div class="demo t-skew">skew</div> 393 <div class="demo t-combo">组合变换</div> 394 </div> 395</section> 396 397<!-- ==================== 2. 3D 变换 ==================== --> 398<section> 399 <h2>2. 3D 变换:rotateX / rotateY / rotateZ / translateZ / perspective</h2> 400 <p class="hint"> 401 父元素设 <code>perspective</code>,立方体设 <code>transform-style: preserve-3d</code>, 402 六个面各自用 <code>translateZ(60px)</code> 推开半个边长。右侧卡片悬停时叠加 <code>rotateY + rotateX + translateZ</code>。 403 </p> 404 <div class="stage"> 405 <div class="scene"> 406 <div class="cube"> 407 <div class="face front">前面</div> 408 <div class="face back">后面</div> 409 <div class="face right">右面</div> 410 <div class="face left">左面</div> 411 <div class="face top">上面</div> 412 <div class="face bottom">下面</div> 413 </div> 414 </div> 415 416 <div class="tilt-wrap"> 417 <div class="tilt-card">悬停我<br>rotateY + rotateX<br>+ translateZ</div> 418 </div> 419 </div> 420</section> 421 422<!-- ==================== 3. 变形原点 ==================== --> 423<section> 424 <h2>3. 变形原点:transform-origin</h2> 425 <p class="hint"> 426 三个方块使用<strong>完全相同的旋转动画</strong>,只有 <code>transform-origin</code> 不同, 427 旋转的支点就不一样。 428 </p> 429 <div class="stage stage-origin"> 430 <div class="origin-box o-center">origin: 50% 50%</div> 431 <div class="origin-box o-top-left">origin: 0 0</div> 432 <div class="origin-box o-bottom-right">origin: 100% 100%</div> 433 </div> 434</section> 435 436<!-- ==================== 4. 过渡 ==================== --> 437<section> 438 <h2>4. 过渡:transition</h2> 439 <p class="hint"> 440 <code>transition</code> 需要状态变化(如 <code>:hover</code>)来触发,不能自动循环。 441 多个属性用逗号分隔,可分别设置时长与缓动。 442 </p> 443 <div class="stage"> 444 <div class="tr-card"> 445 <h4>悬停这张卡片</h4> 446 <p>transform / box-shadow / background-color 三条过渡同时进行</p> 447 </div> 448 449 <div> 450 <p class="hint" style="margin:0 0 4px">transition-delay 制造交错节奏</p> 451 <div class="dots"> 452 <span class="dot"></span> 453 <span class="dot"></span> 454 <span class="dot"></span> 455 <span class="dot"></span> 456 </div> 457 </div> 458 </div> 459</section> 460 461<!-- ==================== 5. 关键帧动画 ==================== --> 462<section> 463 <h2>5. 动画:animation 与 @keyframes</h2> 464 <p class="hint"> 465 <code>@keyframes</code> 定义关键帧,<code>animation</code> 把它应用到元素上。 466 三个例子分别用了不同的关键帧结构:多百分比、0%/50%/100%、以及只有 <code>to</code>。 467 </p> 468 <div class="stage"> 469 <div class="pulse"></div> 470 <div class="bounce"></div> 471 <div class="spin-ring"></div> 472 </div> 473</section> 474 475<!-- ==================== 6. 动画属性 ==================== --> 476<section> 477 <h2>6. 动画属性:duration / timing-function / delay / iteration-count / direction / fill-mode / play-state</h2> 478 <p class="hint"> 479 拖动或切换下面的控件,实时观察方块动画的变化。把 <code>iteration-count</code> 改成 1, 480 再切换 <code>fill-mode</code>,就能直观看到填充模式的区别。 481 </p> 482 483 <div class="controls"> 484 <label> 485 <span>animation-duration <output id="durationOut">2s</output></span> 486 <input type="range" id="duration" min="0.4" max="4" step="0.1" value="2"> 487 </label> 488 489 <label> 490 <span>animation-delay <output id="delayOut">0s</output></span> 491 <input type="range" id="delay" min="0" max="2" step="0.1" value="0"> 492 </label> 493 494 <label> 495 <span>animation-timing-function</span> 496 <select id="timing"> 497 <option value="linear">linear</option> 498 <option value="ease">ease</option> 499 <option value="ease-in">ease-in</option> 500 <option value="ease-out">ease-out</option> 501 <option value="ease-in-out" selected>ease-in-out</option> 502 <option value="cubic-bezier(.68,-0.55,.27,1.55)">cubic-bezier 回弹</option> 503 <option value="steps(6, end)">steps(6, end)</option> 504 </select> 505 </label> 506 507 <label> 508 <span>animation-iteration-count</span> 509 <select id="count"> 510 <option value="1">1</option> 511 <option value="2">2</option> 512 <option value="3">3</option> 513 <option value="infinite" selected>infinite</option> 514 </select> 515 </label> 516 517 <label> 518 <span>animation-direction</span> 519 <select id="direction"> 520 <option value="normal" selected>normal</option> 521 <option value="reverse">reverse</option> 522 <option value="alternate">alternate</option> 523 <option value="alternate-reverse">alternate-reverse</option> 524 </select> 525 </label> 526 527 <label> 528 <span>animation-fill-mode</span> 529 <select id="fill"> 530 <option value="none" selected>none</option> 531 <option value="forwards">forwards</option> 532 <option value="backwards">backwards</option> 533 <option value="both">both</option> 534 </select> 535 </label> 536 </div> 537 538 <div class="ctrl-track"> 539 <div class="ctrl-box" id="ctrlBox">动画</div> 540 </div> 541 542 <div class="btns"> 543 <button id="playBtn">⏸ 暂停</button> 544 <button id="replayBtn">↻ 重播</button> 545 </div> 546</section> 547 548<!-- ==================== 7. 性能优化 ==================== --> 549<section> 550 <h2>7. 性能优化:will-change、transform、opacity</h2> 551 <p class="hint"> 552 上面方块只动画 <code>transform</code> 和 <code>opacity</code>,并声明了 553 <code>will-change</code>,浏览器可以走合成层;下面方块动画 <code>left</code> 和 554 <code>width</code>,每一帧都要重新布局,更容易掉帧。打开 DevTools 的 555 Performance / Rendering 面板对比更明显。 556 </p> 557 558 <div class="perf-item"> 559 <div class="name">✅ transform + opacity(推荐,只触发合成)</div> 560 <div class="perf-track"> 561 <div class="perf-box good"></div> 562 </div> 563 </div> 564 565 <div class="perf-item"> 566 <div class="name">⚠️ left + width(反例,触发 Layout + Paint)</div> 567 <div class="perf-track"> 568 <div class="perf-box bad"></div> 569 </div> 570 </div> 571</section> 572 573<script> 574(function () { 575 var box = document.getElementById('ctrlBox'); 576 var playBtn = document.getElementById('playBtn'); 577 var replayBtn = document.getElementById('replayBtn'); 578 579 var fields = { 580 duration : document.getElementById('duration'), 581 delay : document.getElementById('delay'), 582 timing : document.getElementById('timing'), 583 count : document.getElementById('count'), 584 direction: document.getElementById('direction'), 585 fill : document.getElementById('fill') 586 }; 587 588 var outs = { 589 duration: document.getElementById('durationOut'), 590 delay : document.getElementById('delayOut') 591 }; 592 593 /* 把控件值写入元素的行内样式 */ 594 function apply() { 595 box.style.animationDuration = fields.duration.value + 's'; 596 box.style.animationDelay = fields.delay.value + 's'; 597 box.style.animationTimingFunction = fields.timing.value; 598 box.style.animationIterationCount = fields.count.value; 599 box.style.animationDirection = fields.direction.value; 600 box.style.animationFillMode = fields.fill.value; 601 602 outs.duration.textContent = fields.duration.value + 's'; 603 outs.delay.textContent = fields.delay.value + 's'; 604 } 605 606 Object.keys(fields).forEach(function (key) { 607 fields[key].addEventListener('input', apply); 608 }); 609 apply(); 610 611 /* animation-play-state:播放 / 暂停 */ 612 playBtn.addEventListener('click', function () { 613 var isPaused = box.style.animationPlayState === 'paused'; 614 box.style.animationPlayState = isPaused ? 'running' : 'paused'; 615 playBtn.textContent = isPaused ? '⏸ 暂停' : '▶ 播放'; 616 }); 617 618 /* 重播:先移除 animation-name 强制重排,再恢复 */ 619 replayBtn.addEventListener('click', function () { 620 box.style.animationName = 'none'; 621 void box.offsetWidth; // 触发一次重排,重置动画状态 622 box.style.animationName = 'demoMove'; 623 box.style.animationPlayState = 'running'; 624 playBtn.textContent = '⏸ 暂停'; 625 }); 626})(); 627</script> 628 629</body> 630</html> 631
《【技术专题】CSS3 网页技术 - CSS3 变换与动画》 是转载文章,点击查看原文。

