css3動(dòng)畫效果小結(jié)(推薦)

css3的動(dòng)畫功能有以下三種:
1、transition(過(guò)度屬性)
2、animation(動(dòng)畫屬性)
3、transform(2D/3D轉(zhuǎn)換屬性)
下面逐一進(jìn)行介紹我的理解:
1、transition:<過(guò)渡屬性名稱> <過(guò)渡時(shí)間> <過(guò)渡模式>
如-webkit-transition:color 1s;
等同于:
-webkit-transition-property:color;
-webkit-transition-duration:1s;
多個(gè)屬性的過(guò)渡效果可以這樣寫:
方法1:-webkit-transition:<屬性1> <時(shí)間1> ,<屬性2> <時(shí)間2> ,。。。
方法2:
-webkit-transition:<屬性1> <時(shí)間1>;
-webkit-transition:<屬性2> <時(shí)間2>;
transition-timing-function屬性值有5個(gè):
ease:緩慢開始,緩慢結(jié)束
liner:勻速
ease-in:緩慢開始
ease-out:緩慢結(jié)束
ease-in-out:緩慢開始,緩慢結(jié)束(和ease稍有區(qū)別)
實(shí)例:
transition過(guò)渡效果
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>transition過(guò)渡效果</title>
- <style>
- *{
- margin: 0px;
- padding: 0px;
- }
- #box{
- width: 200px;
- height: 200px;
- background-color: chocolate;
- position: relative;
- left: 0px;
- top: 0px;
- transition: top 5s ease,left 5s ease ;
- -moz-transition: top 5s ease,left 5s ease ; /* Firefox 4 */
- -webkit-transition: top 5s ease,left 5s ease ; /* Safari and Chrome */
- -o-transition: top 5s ease,left 5s ease ; /* Opera */
- }
- .btn{
- width: 512px;
- margin: 0 auto;
- border: 2px solid #e3e3e3;
- border-radius: 5px;
- padding: 10px;
- }
- .btn button{
- width: 80px;
- height: 40px;
- text-align: center;
- line-height: 40px;
- margin-right: 20px;
- }
- button:last-child{
- margin-right: 0px;
- }
- </style>
- <script>
- window.onload=function(){
- var e1 = document.getElementById("e1");
- var e2 = document.getElementById("e2");
- var e3 = document.getElementById("e3");
- var e4 = document.getElementById("e4");
- var e5 = document.getElementById("e5");
- var box = document.getElementById("box");
- e1.onclick=function(){
- box.style.left = 1000+"px";
- box.style.top = 100+"px";
- box.style.transitionTimingFunction="ease";
- };
- e2.onclick=function(){
- box.style.right = 0+"px";
- box.style.top = 0+"px";
- box.style.transitionTimingFunction="liner";
- };
- e3.onclick=function(){
- box.style.right = 1000+"px";
- box.style.top = 100+"px";
- box.style.transitionTimingFunction="ease-in";
- };
- e4.onclick=function(){
- box.style.left = 0+"px";
- box.style.top = 0+"px";
- box.style.transitionTimingFunction="ease-out";
- };
- e5.onclick=function(){
- box.style.left = 1000+"px";
- box.style.top = 100+"px";
- box.style.transitionTimingFunction="ease-in-out";
- };
- }
- </script>
- </head>
- <body>
- <div id="box"></div>
- <br>
- <br>
- <br>
- <br>
- <br>
- <br>
- <hr>
- <br>
- <br>
- <br>
- <div class="btn">
- <button id="e1">ease</button>
- <button id="e2">liner</button>
- <button id="e3">ease-in</button>
- <button id="e4">ease-out</button>
- <button id="e5">ease-in-out</button>
- </div>
- </body>
- </html>
2、動(dòng)畫屬性animation
animation: name duration timing-function delay iteration-count direction;
值 |
描述 |
animation-name |
規(guī)定需要綁定到選擇器的 keyframe 名稱。。 |
animation-duration |
規(guī)定完成動(dòng)畫所花費(fèi)的時(shí)間,以秒或毫秒計(jì)。 |
animation-timing-function |
規(guī)定動(dòng)畫的速度曲線。 |
animation-delay |
規(guī)定在動(dòng)畫開始之前的延遲。 |
animation-iteration-count |
規(guī)定動(dòng)畫應(yīng)該播放的次數(shù)。 |
animation-direction |
規(guī)定是否應(yīng)該輪流反向播放動(dòng)畫。 |
注釋:Internet Explorer 9 以及更早的版本不支持 animation 屬性。
@keyframes animationname {keyframes-selector {css-styles;}}
值 |
描述 |
animationname |
必需。定義動(dòng)畫的名稱。 |
keyframes-selector |
必需。動(dòng)畫時(shí)長(zhǎng)的百分比。 合法的值:
|
css-styles |
必需。一個(gè)或多個(gè)合法的 CSS 樣式屬性。 |
以百分比來(lái)規(guī)定改變發(fā)生的時(shí)間,或者通過(guò)關(guān)鍵詞 "from" 和 "to",等價(jià)于 0% 和 100%。
0% 是動(dòng)畫的開始時(shí)間,100% 動(dòng)畫的結(jié)束時(shí)間。
例如:
- animation:mymove 5s infinite;
- @keyframes mymove{
- from{ top:0px; }
- to{ top:200px; }
- }
還可以這么寫:
- @keyframes mymove{
- 0%{ top:0px; }
- 25%{ top:200px; }
- 50%{ top:100px; }
- 75%{ top:200px; }
- 100%{ top:0px; }
- }
案例:
css3的animation效果
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- div
- {
- width:100px;
- height:100px;
- background:red;
- position:relative;
- animation:mymove 5s infinite;
- -moz-animation:mymove 5s infinite; /* Firefox */
- -webkit-animation:mymove 5s infinite; /* Safari and Chrome */
- -o-animation:mymove 5s infinite; /* Opera */
- }
- @keyframes mymove
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-moz-keyframes mymove /* Firefox */
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-webkit-keyframes mymove /* Safari and Chrome */
- {
- from {top:0px;}
- to {top:200px;}
- }
- @-o-keyframes mymove /* Opera */
- {
- from {top:0px;}
- to {top:200px;}
- }
- </style>
- </head>
- <body>
- <p><b>注釋:</b>本例在 Internet Explorer 中無(wú)效。</p>
- <div></div>
- </body>
- </html>
3、設(shè)置3D場(chǎng)景(即transform)
-webkit-perspective:800;(單位為像素)--即三維物體距離屏幕的距離。
-webkit-perspective-origin:50% 50%;(這個(gè)屬性代表了人眼觀察的視野。50% 50%為X軸、Y軸相應(yīng)的位置,即屏幕的正中央。)
使用transform屬性調(diào)整元素:-webkit-transform-style:-webkit-perserve-3d;(這個(gè)屬性是告訴瀏覽器我們是在一個(gè)三維空間中對(duì)元素進(jìn)行操作)
(1)、translate(移動(dòng)距離)
translateX(x px)
translateY(y px)
translateZ(z px)
(2)、rotate(旋轉(zhuǎn)角度)
rotateX(x deg)
rotateY(y deg)
rotateZ(z deg)
transform:rotate(45deg)
rotateX:向屏幕上邊沿向內(nèi)旋轉(zhuǎn)為正方向。
rotateY:向屏幕豎直向下為正方向。
rotateZ:向屏幕外為正方向。
一個(gè)div塊,右邊沿向屏幕內(nèi)旋轉(zhuǎn)45deg,即應(yīng)設(shè)置為:Transform:rotateY(45deg)。
實(shí)例:
transform3D轉(zhuǎn)換效果
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>transform3D轉(zhuǎn)換效果</title>
- <style>
- *{
- margin: 0px;
- padding: 0px;
- }
- #box{
- width: 200px;
- height: 200px;
- background-color: chocolate;
- position: relative;
- left: 0px;
- top: 0px;
- perspective:800px;
- perspective-origin:50% 50%;
- transform-style: preserve-3d;
- transform-origin:0% 100%;//以Y軸為旋轉(zhuǎn)中心
- }
- p{
- margin:20px 520px;
- }
- .btn{
- width: 300px;
- margin: 0 auto;
- border: 2px solid #e3e3e3;
- border-radius: 5px;
- padding: 10px;
- }
- .btn button{
- width: 80px;
- height: 40px;
- text-align: center;
- line-height: 40px;
- margin-right: 20px;
- }
- button:last-child{
- margin-right: 0px;
- }
- </style>
- <script>
- window.onload=function(){
- var tx = document.getElementById("tx");
- var ty = document.getElementById("ty");
- var tz = document.getElementById("tz");
- var rx = document.getElementById("rx");
- var ry = document.getElementById("ry");
- var rz = document.getElementById("rz");
- var box = document.getElementById("box");
- tx.onclick=function(){
- box.style.transform = "translateX(500px)";
- };
- ty.onclick=function(){
- box.style.transform = "translateY(400px)"
- };
- rx.onclick=function(){
- box.style.transform = "rotateX(30deg)"
- };
- ry.onclick=function(){
- box.style.transform = "rotateY(30deg)"
- };
- rz.onclick=function(){
- box.style.transform = "rotateZ(30deg)"
- };
- }
- </script>
- </head>
- <body>
- <div id="box"></div>
- <br>
- <br>
- <br>
- <br>
- <br>
- <br>
- <hr>
- <br>
- <br>
- <br>
- <p>translate(移動(dòng)距離)</p>
- <div class="btn">
- <button id="tx">translateX</button>
- <button id="ty">translateY</button>
- </div>
- <p>rotate(旋轉(zhuǎn)角度)</p>
- <div class="btn">
- <button id="rx">rotateX</button>
- <button id="ry">rotateY</button>
- <button id="rz">rotateZ</button>
- </div>
- </body>
- </html>
使用transform-origin屬性調(diào)整旋轉(zhuǎn)中心。默認(rèn)旋轉(zhuǎn)中心點(diǎn)為div盒子的正中心。
這個(gè)旋轉(zhuǎn)中心是可以改變的:
X軸:left、center、right.
Y軸:top、center、bottom.
Z軸:length px(一個(gè)長(zhǎng)度值)。
以上這篇css3動(dòng)畫效果小結(jié)(推薦)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
原文地址:http://www.cnblogs.com/gaotenglong/archive/2016/07/24/5700997.html
相關(guān)文章
純CSS3 3D魔方翻轉(zhuǎn)動(dòng)畫特效源碼
純CSS3 3D魔方翻轉(zhuǎn)動(dòng)畫特效源碼是一款使用簡(jiǎn)單純CSS3代碼實(shí)現(xiàn)的魔方特效下載,效果非常棒,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有需要的朋友可以直接下載使用2016-08-10CSS3鼠標(biāo)滑過(guò)圖片標(biāo)題和遮罩層動(dòng)畫特效源碼
本特效是一款使用CSS3制作的簡(jiǎn)單的鼠標(biāo)滑過(guò)圖片標(biāo)題和遮罩層動(dòng)畫特效的代碼,通過(guò)CSS3 transitions和transform屬性實(shí)現(xiàn)2016-08-04純CSS3繪制的哆啦A夢(mèng)頭像動(dòng)畫效果源碼
這是一款使用純CSS3繪制的哆啦A夢(mèng)頭像動(dòng)畫效果源碼。圖形采用純css3繪制,且鼠標(biāo)滑過(guò)圖片中哆啦A夢(mèng)的眼睛、胡子和鈴鐺的時(shí)候會(huì)出現(xiàn)相應(yīng)的動(dòng)畫效果2016-08-04CSS3實(shí)現(xiàn)滾動(dòng)條動(dòng)畫效果代碼分享
本文給大家介紹CSS3實(shí)現(xiàn)動(dòng)畫滾動(dòng)條代碼分享的全部?jī)?nèi)容,代碼簡(jiǎn)單易懂,功能非常的實(shí)用,下面小編給大家分享下2016-08-03純CSS3實(shí)現(xiàn)鼠標(biāo)滑過(guò)圓形圖片旋轉(zhuǎn)翻蓋動(dòng)畫特效源碼
純CSS3實(shí)現(xiàn)鼠標(biāo)滑過(guò)圓形圖片旋轉(zhuǎn)翻蓋動(dòng)畫特效源碼是一款效果非常炫酷的純CSS3圓形圖片鼠標(biāo)滑過(guò)旋轉(zhuǎn)翻蓋特效,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有需要的朋友可以直接下載使用2016-07-06CSS3實(shí)現(xiàn)的鼠標(biāo)滑過(guò)邊框線條動(dòng)畫特效源碼
是一段實(shí)現(xiàn)了鼠標(biāo)經(jīng)過(guò)或懸停一個(gè)選項(xiàng)卡上時(shí),該選項(xiàng)卡周圍會(huì)出現(xiàn)從一點(diǎn)開始到另一點(diǎn)結(jié)束的邊框線條動(dòng)畫效果,非常干凈、時(shí)尚,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有興趣的朋友們2016-07-04- 這篇文章主要為大家詳細(xì)介紹了CSS3中Transform動(dòng)畫屬性用法,教大家如何實(shí)現(xiàn)2D transform變換、3D transform變換,感興趣的小伙伴們可以參考一下2016-07-04
2016奧運(yùn)會(huì)小人騎自行車CSS3動(dòng)畫特效源碼
2016奧運(yùn)會(huì)小人騎自行車CSS3動(dòng)畫特效源碼是一款個(gè)性的卡通小人騎著自行車踩踏板動(dòng)畫特效下載。本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有需要的朋友可以直接下載使用2016-08-15