LESSON-13 · TOPIC 02

transform/opacity 效能

能以 transform 與 opacity 完成位移/淡入,並驗證元素 layout rect 尺寸未因動畫 property 重排。

code-edit過場、動畫與停止條件|transform/opacity 效能本頁只練一個責任

本頁只練什麼

能以 transform 與 opacity 完成位移/淡入,並驗證元素 layout rect 尺寸未因動畫 property 重排。

持續動畫 left/top/width 可能觸發 layout/paint;transform/opacity 通常較適合交由 compositor 處理,但仍需量測而非絕對保證。

要修改的檔案:files/starter/style.css

定位:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。|依 starter-2 定位,只修改「transform/opacity 效能」的責任。

任務情境

先結果,再原理;完成後要能說出自己看見的證據。

現在的問題

通知卡用 left 從0移到24px,每次 hover 都讓周邊 layout 工作增加。

起始狀態:卡片 transform:none、opacity:.6;仍在 normal flow。

完成後要看到

現有 runtime transform 由 none 變 matrix;required facts layoutStable=true 且 midpoint/after 可觀察。

驗收證據:before=none、after包含matrix、layoutStable=true,opacity/transform midpoint可觀察

何時會用到

卡片 hover、toast 進場或小型 CTA 位移淡入時。

常見混淆:transform 不會替元素原位置保留之外的新空間;視覺移動可能覆蓋鄰近內容。

觀念與最小範例

這段程式是理解起點,不是要你直接跳過 Starter。

transform/opacity 效能 Transform and opacity animation performance

讓卡片移動時,優先搬動畫畫面層,而不是每一格都重新安排整個版面。

正式說法:Transforms alter visual coordinate mapping without changing normal-flow layout geometry;opacity affects compositing. Browser optimization is implementation-dependent and should be verified with performance tools。

.toast{transform:translateX(1.5rem);opacity:.6;transition:transform .2s,opacity .2s}.toast.is-in{transform:none;opacity:1}

三步完成本主題

每一步都留下可觀察結果;如果沒看到,先看「卡住先查」。

  1. Step 1|先看見目前缺口,不急著貼答案。

    檔案:files/starter/style.css 定位:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。

    要做:先重新載入 Starter,記錄目前畫面、DOM、Computed、Console 或文件內容。

    預期:能指出「先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。」目前的缺口。

    證據:保存 files/starter/style.css 的目前狀態,並指出下一步只會修改哪個檔案責任。

    卡住先查:確認開啟的是 files/starter/style.css,且定位到 先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。。

  2. Step 2|只修改本主題的一個責任。

    檔案:files/starter/style.css 定位:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。

    要做:執行實驗切換 motion-safe class,讀 computed transform;完整 probe 比較 rect 尺寸與 lifecycle。

    預期:現有 runtime transform 由 none 變 matrix;required facts layoutStable=true 且 midpoint/after 可觀察。

    證據:before=none、after包含matrix、layoutStable=true,opacity/transform midpoint可觀察

    卡住先查:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。

  3. Step 3|重新載入並以證據驗收,不以『看起來差不多』判定完成。

    檔案:files/starter/style.css 定位:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。

    要做:重新整理頁面,再逐項比對預期結果與 evidence。

    預期:現有 runtime transform 由 none 變 matrix;required facts layoutStable=true 且 midpoint/after 可觀察。

    證據:before=none、after包含matrix、layoutStable=true,opacity/transform midpoint可觀察

    卡住先查:先記錄元素 bounding rect 與 Computed transform/opacity,不先開啟動畫。

觀察示範

這個示範只讓你看懂概念,不會替你修改 Starter,也不會自動宣告完成。

transform/opacity 效能runtime fixture · read-only demo
本主題示範畫面:
儲存成功

驗收證據

勾選只是學習紀錄;真正完成仍要回到 Starter 的實際結果。

提示 1|方向

把 left 位移改成 translateX。

提示 2|關鍵片段

transition 只列 transform 與 opacity,不用 all。

提示 3|完整解答與原因

完整解答與原因:translateX 改視覺座標而不重算 normal-flow 配置;opacity 可同時淡入,但仍以 layoutStable/performance facts驗證。

完成後進入下一階段,或回到入口選下一個 topic。

開始 Starter →回到主題清單