小白教程
所有教程
关于
Search
172.70.127.65
172.70.127.65
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“CSS3 过渡”的源代码
本页内容
上一节:
CSS3_3D_转换
下一节:
CSS3_动画
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:CSS3 过渡}}[[Category:CSS 教程|51]] = CSS3 过渡 = == CSS3 过渡 == CSS3中,我们为了添加某种效果可以从一种样式转变到另一个的时候,无需使用Flash动画或JavaScript。用鼠标移过下面的元素: 用鼠标移过下面的元素: CSS3过渡 == 浏览器支持 == 表格中的数字表示支持该属性的第一个浏览器版本号。 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号。 {| class="table table-striped table-hover" ! 属性 ! Chrome ! Internet Explorer / Edge ! Firefox ! Safari ! Opera |- | transition | 26.04.0 -webkit- | 10.0 | 16.04.0 -moz- | 6.13.1 -webkit- | 12.110.5 -o- |- | transition-delay | 26.04.0 -webkit- | 10.0 | 16.04.0 -moz- | 6.13.1 -webkit- | 12.110.5 -o- |- | transition-duration | 26.04.0 -webkit- | 10.0 | 16.04.0 -moz- | 6.13.1 -webkit- | 12.110.5 -o- |- | transition-property | 26.04.0 -webkit- | 10.0 | 16.04.0 -moz- | 6.13.1 -webkit- | 12.110.5 -o- |- | transition-timing-function | 26.04.0 -webkit- | 10.0 | 16.04.0 -moz- | 6.13.1 -webkit- | 12.110.5 -o- |} == 它是如何工作? == CSS3 过渡是元素从一种样式逐渐改变为另一种的效果。 要实现这一点,必须规定两项内容: * 指定要添加效果的CSS属性 * 指定效果的持续时间。 应用于宽度属性的过渡效果,时长为 2 秒: <sample title="" desc="" lang="html" hererun="1"> div { transition: width 2s; -webkit-transition: width 2s; /* Safari */ } </sample> '''注意:''' 如果未指定的期限,transition将没有任何效果,因为默认值是0。 指定的CSS属性的值更改时效果会发生变化。一个典型CSS属性的变化是用户鼠标放在一个元素上时: 规定当鼠标指针悬浮(:hover)于 <div>元素上时: <sample title="" desc="" lang="html" hererun="1"> div:hover { width:300px; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> div { width:100px; height:100px; background:red; transition:width 2s; -webkit-transition:width 2s; /* Safari */ } div:hover { width:300px; } </style> </head> <body> <p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div></div> <p>鼠标移动到 div 元素上,查看过渡效果。</p> </body> </html> </run> '''注意:''' 当鼠标光标移动到该元素时,它逐渐改变它原有样式 == 多项改变 == 要添加多个样式的变换效果,添加的属性由逗号分隔: 添加了宽度,高度和转换效果: <sample title="" desc="" lang="html" hererun="1"> div { transition: width 2s, height 2s, transform 2s; -webkit-transition: width 2s, height 2s, -webkit-transform 2s; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> div { width: 100px; height: 100px; background: red; -webkit-transition: width 2s, height 2s, -webkit-transform 2s; /* For Safari 3.1 to 6.0 */ transition: width 2s, height 2s, transform 2s; } div:hover { width: 200px; height: 200px; -webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */ transform: rotate(180deg); } </style> </head> <body> <p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div>鼠标移动到 div 元素上,查看过渡效果。</div> </body> </html> </run> == 过渡属性 == 下表列出了所有的过渡属性: {| class="table table-striped table-hover" ! 属性 ! 描述 ! CSS |- | [[css3 pr transition|transition]] | 简写属性,用于在一个属性中设置四个过渡属性。 | 3 |- | [[css3 pr transition property|transition-property]] | 规定应用过渡的 CSS 属性的名称。 | 3 |- | [[css3 pr transition duration|transition-duration]] | 定义过渡效果花费的时间。默认是 0。 | 3 |- | [[css3 pr transition timing function|transition-timing-function]] | 规定过渡效果的时间曲线。默认是 "ease"。 | 3 |- | [[css3 pr transition delay|transition-delay]] | 规定过渡效果何时开始。默认是 0。 | 3 |} 下面的两个例子设置所有过渡属性: 在一个例子中使用所有过渡属性: <sample title="" desc="" lang="html" hererun="1"> div { transition-property: width; transition-duration: 1s; transition-timing-function: linear; transition-delay: 2s; /* Safari */ -webkit-transition-property:width; -webkit-transition-duration:1s; -webkit-transition-timing-function:linear; -webkit-transition-delay:2s; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> div { width:100px; height:100px; background:red; transition-property:width; transition-duration:1s; transition-timing-function:linear; transition-delay:2s; /* Safari */ -webkit-transition-property:width; -webkit-transition-duration:1s; -webkit-transition-timing-function:linear; -webkit-transition-delay:2s; } div:hover { width:200px; } </style> </head> <body> <p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div></div> <p>鼠标移动到 div 元素上,查看过渡效果。</p> <p><b>注意:</b> 过渡效果需要等待两秒后才开始。</p> </body> </html> </run> 与上面的例子相同的过渡效果,但是使用了简写的 transition 属性: <sample title="" desc="" lang="html" hererun="1"> div { transition: width 1s linear 2s; /* Safari */ -webkit-transition:width 1s linear 2s; } </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style> div { width:100px; height:100px; background:red; transition:width 1s linear 2s; /* Safari */ -webkit-transition:width 1s linear 2s; } div:hover { width:200px; } </style> </head> <body> <p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div></div> <p>鼠标移动到 div 元素上,查看过渡效果。</p> <p><b>注意:</b> 过渡效果需要等待两秒后才开始。</p> </body> </html> </run>
返回至“
CSS3 过渡
”。
上一节:
CSS3_3D_转换
下一节:
CSS3_动画