English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The CSS3 transition feature allows the change of CSS property values to occur smoothly over the specified duration.
Usually, when the value of a CSS property changes, the rendering result is updated immediately. A common example is changing the background color of a button when the mouse hovers over it. Normally, when the cursor is placed on the button, the background color of the button changes immediately from the old property value to the new property value.
CSS3 introduces a new transition feature that allows you to smoothly animate properties from old values to new values over time.
The following examples will show you howbackground-colorAnimation on HTML button on mouse hover.
button { background: #fd7c2a; /* For Safari 3.0+ */ -webkit-transition-property: background; -webkit-transition-duration: 2s; transition-property: background; transition-duration: 2s; } button:hover { background: #3cc16e; }测试看看‹/›
You must specify at least two properties, transition-property and transition-duration (greater than 0), to make them produce a transition effect. However, all otherTransition propertiesThey are optional because their default values will not prevent the transition from occurring.
注意:Not all CSS properties are animatable. Usually, any CSS property that accepts numeric, length, percentage, or color values is animatable.
Each transition property can take multiple values, separated by commas, which provides a simple way to define multiple transitions with different settings at once.
button { background: #fd7c2a; border: 3px solid #dc5801; /* For Safari 3.0+ */ -webkit-transition-property: background, border; -webkit-transition-duration: 1s, 2s; transition-property: background, border; transition-duration: 1s, 2s; } button:hover { background: #3cc16e; border-color: #288049; }测试看看‹/›
When applying transitions, many properties must be considered. However, all transition properties can also be specified in a single property to shorten the code.
过渡属性是一种简写属性,用于按列出的顺序一次设置所有单个过渡属性(即缩写属性transition-property,transition-duration,transition-timing-function和transition-delay在所列出的顺序)。
使用此属性时,请务必对值遵循此顺序。
button { background: #fd7c2a; -webkit-transition: background 2s ease-in 0s; /* For Safari 3.0+ */ transition: background 2s ease-in 0s; } button:hover { background: #3cc16e; }测试看看‹/›
注意:如果缺少或未指定任何值,则将使用该属性的默认值。这意味着,如果transition-duration缺少属性的值,则不会发生过渡,因为其默认值为0。
下表简要概述了所有过渡属性:
属性 | 描述 |
---|---|
transition | 用于在单个声明中设置所有四个单独过渡属性的简写属性。 |
transition-delay | 指定何时开始过渡。 |
transition-duration | 指定过渡动画完成所需的秒数或毫秒数。 |
transition-property | 指定应将过渡效果应用到的CSS属性的名称。 |
transition-timing-function | 指定如何计算受过渡影响的CSS属性的中间值。 |