萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS Transition 完整屬性表與 Timing Function 詳解

transition 讓 CSS 屬性值變化時產生平滑動畫,語法四要素:property、duration、timing-function、delay。

完整語法

.element {
  -webkit-transition: property duration timing-function delay;
  -moz-transition:    property duration timing-function delay;
  -o-transition:      property duration timing-function delay;
  transition:         property duration timing-function delay;
}

可動畫屬性清單(部分)

屬性值類型
color顏色
background-color顏色
opacity數值 0~1
width / height長度、百分比
margin / padding長度、百分比
transform變換函數
font-size / line-height長度、百分比、數值
letter-spacing / word-spacing長度
text-indent長度、百分比
border-width / outline-width長度
z-index整數
visibility可見性關鍵字

不可動畫:display、position、float、background-image(漸變視為圖片)等離散值屬性。

Timing Function(緩動函數)

關鍵字曲線特性適用場景
linear勻速機械感、無限循環
ease慢→快→慢(預設)大多數 UI 互動
ease-in加速(漸入)元素進場
ease-out減速(淡出)元素離場、彈窗關閉
ease-in-out加速後減速來回切換
cubic-bezier(x1,y1,x2,y2)自訂貝塞爾曲線品牌專屬動感、彈跳、彈性

實用範例

.btn {
  background: #3498db;
  transition: background 0.3s ease, transform 0.1s ease-out;
}
.btn:hover { background: #2980b9; }
.btn:active { transform: scale(0.98); }

/* 延遲登場 */
.card:nth-child(1) { transition-delay: 0ms; }
.card:nth-child(2) { transition-delay: 100ms; }
.card:nth-child(3) { transition-delay: 200ms; }

效能建議

  • 優先動畫 transform、opacity(合成器執行緒,不觸發重排/重繪)。
  • 避免動畫 width/height/margin/top/left 等佈局屬性。
  • 使用 will-change: transform, opacity; 提前提示瀏覽器(慎用,記得移除)。
copyright © 萬盛學電腦網 all rights reserved