transition
语法
- transition:<single-transition>[,<single-transition>]*
- <single-transition> = [ none | <single-transition-property> ] || <time> || <single-transition-timing-function> || <time>
- 默认值:看每个独立属性
- 适用于:所有元素,包含伪对象:after和:before
- 继承性:无
- 动画性:否
- 计算值:看每个独立属性
- 媒体:交互
取值
- <' transition-property '>:检索或设置对象中的参与过渡的属性
- <' transition-duration '>:检索或设置对象过渡的持续时间
- <' transition-timing-function '>:检索或设置对象中过渡的动画类型
- <' transition-delay '>:检索或设置对象延迟过渡的时间
说明
复合属性。检索或设置对象变换时的过渡。
- 注意:如果只提供一个<time>参数,则为 <' transition-duration '> 的值定义;如果提供二个<time>参数,则第一个为 <' transition-duration '> 的值定义,第二个为 <' transition-delay '> 的值定义
- 可以为同一元素的多个属性定义过渡效果。示例:
缩写方式
transition:
border-color .5s ease-in .1s,
background-color .5s ease-in .1s,
color .5s ease-in .1s;
拆分方式:
transition-property: border-color, background-color, color;
transition-duration: .5s, .5s, .5s;
transition-timing-function: ease-in, ease-in, ease-in;
transition-delay: .1s, .1s, .1s;
- 如果定义了多个过渡的属性,而其他属性只有一个参数值,则表明所有需要过渡的属性都应用同一个参数值。据此可以对上面的例子进行缩写:
拆分方式:
transition-property: border-color, background-color, color;
transition-duration: .5s;
transition-timing-function: ease-in;
transition-delay: .1s;
- 如果需要定义多个过渡属性且不想指定具体是哪些属性过渡,同时其他属性只有一个参数值,据此可以对上面的例子进行缩写:
缩写方式
transition: all .5s ease-in .1s;
拆分方式:
transition-property:all;
transition-duration: .5s;
transition-timing-function: ease-in;
transition-delay: .1s;
- 对应的脚本特性为transition。
兼容性
Values | IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|---|
Basic Support | 6.0-9.0 | 2.0-3.6 | 4.0-25.0-webkit- | 6.0-webkit- | 15.0+ | 6.0-6.1-webkit- | 2.1-4.3-webkit- | 18.0-24.0-webkit- |
10.0+ | 4.0-15.0-moz- | 26.0+ | 6.1+ | 7.0+ | 4.4.4+ | 25.0+ | ||
16.0+ |
示例
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<style>
h1{font-size:16px;}
.test{overflow:hidden;width:100%;margin:0;padding:0;list-style:none;}
.test li{float:left;width:100px;height:100px;margin:0 5px;border:1px solid #ddd;background-color:#eee;text-align:center;
-webkit-transition:background-color .5s ease-in;
-moz-transition:background-color .5s ease-in;
transition:background-color .5s ease-in;
}
.test li:nth-child(1):hover{background-color:#bbb;}
.test li:nth-child(2):hover{background-color:#999;}
.test li:nth-child(3):hover{background-color:#630;}
.test li:nth-child(4):hover{background-color:#090;}
.test li:nth-child(5):hover{background-color:#f00;}
</style>
</head>
<body>
<h1>请将鼠标移动到下面的矩形上:</h1>
<ul class="test">
<li>背景色过渡</li>
<li>背景色过渡</li>
<li>背景色过渡</li>
<li>背景色过渡</li>
<li>背景色过渡</li>
</ul>
</body>
</html>