CSS3 過渡
CSS3?過渡
CSS3 過渡
CSS3中,我們?yōu)榱颂砑幽撤N效果可以從一種樣式轉(zhuǎn)變到另一個(gè)的時(shí)候,無需使用Flash動(dòng)畫或JavaScript。
它是如何工作?
CSS3 過渡是元素從一種樣式逐漸改變?yōu)榱硪环N的效果。
要實(shí)現(xiàn)這一點(diǎn),必須規(guī)定兩項(xiàng)內(nèi)容:
指定要添加效果的CSS屬性指定效果的持續(xù)時(shí)間。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>TransitionDemo</title> <style> #wrapper { width: 1024px; margin: 0 auto; } .progress-bar-bg { width: 960px; /*background-color: aliceblue;*/ background-color: lightyellow; } .progress-bar { height: 40px; width: 40px; background-color: #69C; border: 1px solid lightyellow; border-radius: 5px; } .progress-bar:hover { width: 960px; } #bar1 { -webkit-transition: width 5s linear; /*-webkit-transition: width 5s steps(6, end);*/ /*-webkit-transition: width 5s step-start;*/ } #bar2 { -webkit-transition: width 5s ease; } #bar3 { -webkit-transition: width 5s ease-in; } #bar4 { -webkit-transition: width 5s ease-out; } #bar5 { -webkit-transition: width 5s ease-in-out; } </style> </head> <body> <div id="wrapper"> <p>linear</p> <div> <div id="bar1"></div> </div> <p>ease</p> <div id="bar2"></div> <p>ease-in</p> <div id="bar3"></div> <p>ease-out</p> <div id="bar4"></div> <p>ease-in-out</p> <div id="bar5"></div> </div> </body> </html>
多項(xiàng)改變
要添加多個(gè)樣式的變換效果,添加的屬性由逗號(hào)分隔:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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>該實(shí)例無法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div>鼠標(biāo)移動(dòng)到圖標(biāo)上,查看過渡效果。</div> </body> </html>
過渡屬性
下表列出了所有的過渡屬性:
屬性 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??描述 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?CSS
transition ? ?簡(jiǎn)寫屬性,用于在一個(gè)屬性中設(shè)置四個(gè)過渡屬性。 ? ? ? ? ? ? ? ? ? ?3 ? ?
transition-property ? ?規(guī)定應(yīng)用過渡的 CSS 屬性的名稱。 ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ?
transition-duration ? ?定義過渡效果花費(fèi)的時(shí)間。默認(rèn)是 0。 ? ? ? ? ? ? ? ? ? ? ?3 ? ?
transition-timing-function ? ?規(guī)定過渡效果的時(shí)間曲線。默認(rèn)是 "ease"。 3 ? ?
transition-delay ? ?規(guī)定過渡效果何時(shí)開始。默認(rèn)是 0。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ?
在一個(gè)例子中使用所有過渡屬性:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</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>該實(shí)例無法在 Internet Explorer 9 及更早 IE 版本上工作。</p> <div></div> <p>鼠標(biāo)移動(dòng)到 div 元素上,查看過渡效果。</p> <p><b>注意:</b> 過渡效果需要等待兩秒后才開始。</p> </body> </html>