transition
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年9月.
* Some parts of this feature may have varying levels of support.
transition CSS 屬性是 transition-property、transition-duration、transition-timing-function 和 transition-delay 的簡寫屬性。
嘗試一下
transition: margin-right 2s; transition: margin-right 2s 0.5s; transition: margin-right 2s ease-in-out; transition: margin-right 2s ease-in-out 0.5s; transition: margin-right 2s, color 1s; transition: all 1s ease-out; <section id="default-example"> <div id="example-element">Hover to see<br />the transition.</div> </section> #example-element { background-color: #e4f0f5; color: #000; padding: 1rem; border-radius: 0.5rem; font: 1em monospace; width: 100%; transition: margin-right 2s; } #default-example:hover > #example-element { background-color: #909; color: #fff; margin-right: 40%; } Transitions 讓你可以定義元件在兩個狀態之間切換的轉場效果。兩個不同狀態可以使用虛擬類別定義,像是:hover 或 :active 亦或是 使用 JavaScript 設定的狀態變化。
語法
css
/* Apply to 1 property */ /* property name | duration */ transition: margin-right 4s; /* property name | duration | delay */ transition: margin-right 4s 1s; /* property name | duration | timing function */ transition: margin-right 4s ease-in-out; /* property name | duration | timing function | delay */ transition: margin-right 4s ease-in-out 1s; /* Apply to 2 properties */ transition: margin-right 4s, color 1s; /* Apply to all changed properties */ transition: all 0.5s ease-out; /* Global values */ transition: inherit; transition: initial; transition: unset; transition 屬性可以包含一個或多個轉場設定,每一項設定都以逗點分開。
每一項設定都描述著每一項屬性所對應的轉場效果(或是 all 及 none這兩個特殊值)。其包含了:
-
無或一個對應的屬性名稱。可以是:
- 關鍵字
none - 關鍵字
all - 以
<custom-ident>命名的 CSS 屬性。
- 關鍵字
-
無或一個
<single-transition-timing-function>表示所使用的漸變函式。 -
無、一個或兩個
<time>值。第一個值會被指派給transition-duration,而第二個則被指派給transition-delay。
當每個轉場設定不等長時,請參見 當 property values list 之間不等長時... 。簡而言之,多餘的描述(超過實際被執行的數量)會直接被忽略。
標準語法
transition =
<single-transition>#
<single-transition> =
[ none | <single-transition-property> ] ||
<time> ||
<easing-function> ||
<time> ||
<transition-behavior-value>
<single-transition-property> =
all |
<custom-ident>
<easing-function> =
<linear-easing-function> |
<cubic-bezier-easing-function> |
<step-easing-function>
<transition-behavior-value> =
normal |
allow-discrete
<linear-easing-function> =
linear |
<linear()>
<cubic-bezier-easing-function> =
ease |
ease-in |
ease-out |
ease-in-out |
<cubic-bezier()>
<step-easing-function> =
step-start |
step-end |
<steps()>
<linear()> =
linear( [ <number> && <percentage>{0,2} ]# )
<cubic-bezier()> =
cubic-bezier( [ <number [0,1]> , <number> ]#{2} )
<steps()> =
steps( <integer> , <step-position>? )
<step-position> =
jump-start |
jump-end |
jump-none |
jump-both |
start |
end
範例
CSS 轉場 有更多範例。
規範
| Specification |
|---|
| CSS Transitions> # transition-shorthand-property> |
| 預設值 | as each of the properties of the shorthand:
|
|---|---|
| Applies to | all elements, ::before and ::after pseudo-elements |
| 繼承與否 | no |
| Computed value | as each of the properties of the shorthand:
|
| Animation type | Not animatable |