詳解Css3新特性應(yīng)用之過渡與動(dòng)畫

一、緩動(dòng)效果
學(xué)習(xí)和利用貝塞爾曲線,默認(rèn)支持ease,ease-in,ease-out,ease-in-out和linear等
還提供一個(gè)cubic-beizer自定義貝塞爾曲線的起點(diǎn)和終點(diǎn)
Css中只支持一條貝塞爾曲的運(yùn)動(dòng),不能連續(xù)多段
對cubic-beizer中控制錨點(diǎn)的水平坐標(biāo)與垂直坐標(biāo)互換,就可以得到任何調(diào)整函數(shù)的反向版本 cubic-beizer(.1,.25,1,.25)是ease的反向調(diào)整函數(shù) 水平坐標(biāo)只能在0~1的范圍內(nèi),因?yàn)樗阶鴺?biāo)表示的是時(shí)間垂直坐標(biāo)可以超過此范圍,表示為運(yùn)動(dòng)距離
示例代碼
<head> <meta charset="UTF-8"> <title>Document</title> <style> @keyframes bounce{ 60%, 80%, to{ transform: translateY(350px); animation-timing-function: ease-out; /*加速*/ } 70%{ transform: translateY(250px); } 90%{ transform: translateY(300px) } } .ball{ display: inline-block; animation: bounce 3s ease-in; /*減速*/ width: 20px; height: 20px; border-radius: 50%; background: red; } @keyframes bounceEase{ 60%, 80%, to{ transform: translateY(400px); animation-timing-function: ease; } 70% { transform: translateY(300); } 90% { transform: translateY(350px); } } .ball02{ display: inline-block; animation: bounceEase 3s cubic-bezier(.1,.25,1,.25);/*反向ease*/ margin-left: 60px; width: 20px; height: 20px; border-radius: 50%; background: gray; } </style> </head> <body> <div class="ball"> </div> <div class="ball02"></div> </body>
利用過渡(transition)實(shí)現(xiàn)
但需要注意transition-property默認(rèn)值為all,所有可以過渡的屬性都會(huì)被過濾
示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> input:not(:focus) + .callout{ transform: scale(0); transition-duration: .25s; /*覆蓋默認(rèn)的.5s時(shí)間*/ transition-property: transform; /*只過渡transform屬性,不過濾背景等其他屬性*/ } .callout{ transition: .5s cubic-bezier(.25,.1,.3,1.5); /*光標(biāo)輸出input時(shí),有動(dòng)畫*/ transition-property: transform;/*只過渡transform屬性,不過濾背景等其他屬性*/ } input{ display: block; } .callout{ background: #fed; position: absolute; max-width: 14em; padding: .6em, .8em; } </style> </head> <body> <label> Your name: <input type="text" id="username" /> <span class="callout">Only letters,number,underscores and hyphens allowed</span> </label> </body>
二、逐幀動(dòng)畫
animation-timing-function中的steps函數(shù),主要用他實(shí)現(xiàn)幀動(dòng)畫,他是一個(gè)階躍函數(shù),共兩個(gè)參數(shù) 參數(shù)一:一個(gè)數(shù)字,代表時(shí)間函數(shù)中的間隔數(shù)量(必須為正數(shù)) timing-function是作用于每兩個(gè)關(guān)鍵幀之間,而不是整個(gè)動(dòng)畫過程
參數(shù)二:接受start和end兩個(gè)值,指定在每個(gè)間隔的起點(diǎn)或是終點(diǎn)發(fā)生階躍變化,默認(rèn)end,step-start和step-end分別是steps(1,start)和steps(1,end)的簡寫
示例代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> @keyframes loader{ to{ background-position: -128px 0; } } .wrap{ background: url("../img/frameAnimate.png") no-repeat; width: 32px; height: 50px; background-position: 0px 0px; animation: loader 1s infinite steps(4); } </style> </head> <body> <div class="wrap"></div> </body>
三、閃爍效果
實(shí)現(xiàn)兩種閃爍效果,一是平滑閃爍,另一種是幀閃爍(更接近于現(xiàn)實(shí))
平滑閃爍
主要是利用animation-iteration-count和animation-direction兩個(gè)屬性實(shí)現(xiàn)。
1.animation-iteration-count:表示動(dòng)畫的執(zhí)行次數(shù)
2.animation-direction:表示動(dòng)畫是否應(yīng)該輪流反向播放動(dòng)畫,如果值為alternate時(shí),animation-iteration-count必須是一個(gè)偶數(shù),因?yàn)槭瞧鏀?shù)正常播放,偶數(shù)反向播放
代碼如下:
<style> @keyframes blink-smooth{ to{ color: transparent; } } .wrap{ animation: 1s blink-smooth; animation-iteration-count: 6; animation-direction: alternate; } </style> <div class="wrap">我是平滑的顯示和隱藏三次</div>
幀閃爍
利用animation-timing-function屬性的steps實(shí)現(xiàn),因steps指定兩個(gè)關(guān)鍵幀之間分成幾個(gè)片段執(zhí)行動(dòng)畫
1.animation-timing-function: steps(1),然后配合上動(dòng)畫在50%實(shí)現(xiàn)一個(gè)透明即可
代碼如下:
<style> @keyframes blink-smooth02{ 50% { color: transparent; } } .wrap02{ animation: 1s blink-smooth02; animation-iteration-count: 3; animation-timing-function: steps(1); } </style> <div class="wrap">我是逐幀的顯示和隱藏三次</div>
四、打字效果(只支持單行英文)
需要利用用下特性:
1.等寬字體,然后加上ch這個(gè)單位,ch是表示'0'這個(gè)字符的寬度.
2.使用動(dòng)畫讓元素寬度從0變到最大寬度。
3.利用steps(1)讓每個(gè)關(guān)鍵幀的地方產(chǎn)生動(dòng)畫 代碼如下:
<head> <meta charset="UTF-8"> <title>Document</title> <style> @keyframes typing { from{ width: 0; } } @keyframes cart{ 50%{ border-color: currentColor; } /*利用steps在關(guān)鍵幀位置發(fā)生動(dòng)畫實(shí)現(xiàn)*/ } .wrap{ width: 14ch; animation: typing 8s steps(14) , cart 1s steps(1) infinite; white-space: nowrap; overflow: hidden; border-right:1px solid transparent; font-family: Courier New, Courier, monospace; } </style> </head> <body> <div class="wrap">Css is awesome</div> </body>
五、狀態(tài)平滑的動(dòng)畫
利用animation-play-state屬性實(shí)現(xiàn)動(dòng)畫的暫停和播放功能,以及改變背景的定位。示例代碼如下:
<head> <meta charset="UTF-8"> <title>Document</title> <style> @keyframes mic{ to{ background-position: 100% 0; } } .wrap{ background: url("../img/cat.png"); background-repeat: no-repeat; background-size: auto 100%; width: 100px; height: 200px; animation: mic 5s linear infinite alternate; /*直線運(yùn)動(dòng),永不停止,偶數(shù)反向運(yùn)動(dòng)*/ animation-play-state: paused; } .wrap:hover, .wrap:active{ animation-play-state: running; } </style> </head> <body> <div class="wrap"></div> </body>
六、沿環(huán)型路徑平移的動(dòng)畫
這點(diǎn)很重要,transform中的變形函數(shù)(如:rotate,transflate等)都是會(huì)影響元素整個(gè)坐標(biāo)系統(tǒng)。也就是說rotate旋轉(zhuǎn)的時(shí)候是旋轉(zhuǎn)的整個(gè)坐標(biāo)系統(tǒng)。這是實(shí)現(xiàn)用一個(gè)元素沿環(huán)弄路徑平移的基礎(chǔ)。原理圖如下:
兩個(gè)元素方案,transform-origin + rotate可以實(shí)現(xiàn),但html結(jié)構(gòu)需要兩個(gè)元素,如下代碼:
<head> <meta charset="UTF-8"> <title>Document</title> <style> @keyframes spin{ to{ transform: rotate(1turn); } /*順時(shí)針旋轉(zhuǎn)360*/ } @keyframes spin-reverse{ from{ transform: rotate(1turn); } /*逆時(shí)針旋轉(zhuǎn)360*/ } .wrap{ width: 300px; height: 300px; background: yellow; border-radius: 50%; overflow: hidden; padding: 20px; /*加大窗口的寬和高,利用背景從邊框開始的原理,讓運(yùn)動(dòng)圖片與邊框有一定的距離*/ } .spin{ width: 30px; height: 30px; border-radius: 50%; overflow: hidden; margin: 0px auto; /*運(yùn)行元素居中*/ animation: spin 5s infinite linear; transform-origin: 50% 150px; /*定位變換的原點(diǎn)*/ } .spin > img{ width: inherit; height: inherit; animation: spin-reverse 5s infinite linear; --animation: inherit; --animation-direction: reverse; /*由于動(dòng)畫會(huì)控制整個(gè)元素+元素內(nèi)部的元素一起動(dòng)畫,所以內(nèi)部的元素要反向動(dòng)畫*/ } </style> </head> <body> <div class="wrap"> <div class="spin"> <img src="../img/cat.png" alt="" /> </div> </div> </body>
說明:
1..spin的transform-origin: 50% 150px;是進(jìn)行變換原點(diǎn)的定位;
2.由于需要實(shí)現(xiàn)spin環(huán)形運(yùn)動(dòng),transform本質(zhì)特性是元素+元素內(nèi)部子元素都要隨著變換,因此需要對img元素進(jìn)行反向變形
3.實(shí)現(xiàn)兩種反向變形的方式:A:寫一個(gè)反向變形動(dòng)畫;B:繼承父級(jí)的動(dòng)畫,用animation-direction指定位reverse進(jìn)行反向。
單個(gè)元素方案,利用translate和rotate(多次利用),html結(jié)構(gòu)只有一層,代碼如下:
<head> <meta charset="UTF-8"> <title>Document</title> <style> /*反向旋轉(zhuǎn)必須有,不然位置不對*/ @keyframes spinc{ from{ transform: translate(50%, 150px) rotate(0turn) translate(-50%, -150px) translate(50%, 50%) rotate(1turn) translate(-50%, -50%); /*前三個(gè)第一輪旋轉(zhuǎn),后三個(gè)第二輪旋轉(zhuǎn)*/ } to{ transform: translate(50%, 150px) rotate(1turn) translate(-50%, -150px) translate(50%, 50%) rotate(0turn) translate(-50%, -50%); } } .wrap{ width: 300px; height: 300px; background: yellow; border-radius: 50%; overflow: hidden; padding: 20px; /*加大窗口的寬和高,利用背景從邊框開始的原理,讓運(yùn)動(dòng)圖片與邊框有一定的距離*/ } .avatar{ width: 30px; height: 30px; border-radius: 50%; overflow: hidden; margin: 0px auto; /*運(yùn)行元素居中*/ display: block; animation: spinc 5s linear infinite; } </style> </head> <body> <div class="wrap"> <img src="../img/cat.png" alt="" class="avatar" /> </div> </body>
說明:
1.一個(gè)img然后即要沿環(huán)型路徑運(yùn)動(dòng),本身又不能隨著旋轉(zhuǎn),那么就需要兩組位移和旋轉(zhuǎn)
2.第一組位移 + 旋轉(zhuǎn),實(shí)現(xiàn)img元素沿環(huán)形路徑運(yùn)動(dòng)
translate(50%, 150px)
rotate(0turn)
translate(-50%, -150px)
3.第二組位移 + 旋轉(zhuǎn),實(shí)現(xiàn)img元素本身定位不動(dòng)
translate(50%, 50%)
rotate(1turn)
translate(-50%, -50%)
兩個(gè)元素方案主單個(gè)元素方案效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
CSS3實(shí)現(xiàn)類似翻書效果的過渡動(dòng)畫的示例代碼
這篇文章主要介紹了CSS3實(shí)現(xiàn)類似翻書效果的過渡動(dòng)畫的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編2019-09-06CSS3實(shí)現(xiàn)的鼠標(biāo)懸停圖片模糊過渡動(dòng)畫特效源碼
這是一款基于CSS3實(shí)現(xiàn)的鼠標(biāo)懸停圖片模糊過渡動(dòng)畫特效源碼。界面中展示了四件服裝商品,當(dāng)鼠標(biāo)滑過商品的時(shí)候,可以看到商品圖片出現(xiàn)模糊化,并快速過渡切換到下一張商品圖2019-07-10- 本文主要介紹了菜單欄 “三” 變形為“X”css3過渡動(dòng)畫的實(shí)現(xiàn)方法。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-28
用css3實(shí)現(xiàn)轉(zhuǎn)換過渡和動(dòng)畫效果
這篇文章主要介紹了用css3實(shí)現(xiàn)轉(zhuǎn)換過渡和動(dòng)畫效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-13