寫在前面
馬上就2020年了,不知道小夥伴們今年學習了css3動畫了嗎?
說起來css動畫是一個很尬的事,一方面因爲公司用css動畫比較少,另一方面大部分開發者習慣了用JavaScript來做動畫,所以就導致了許多程序員比較排斥來學習css動畫(至少我是),但是一個不懂css動畫的前端工程師不能稱之爲掌握css3,其實當你真正學習css動畫之後,你會被它的魅力所吸引的,它可以減少代碼量、提高性能。
上一篇文章我們已經一起學習了transition 的使用,如果有不瞭解的同學可以查看我的上一篇文章。
話不多說,馬上和我一起去學習今天的主角animation吧!
animation 語法
值 | 描述 |
---|---|
@keyframes | 定義一個動畫,@keyframes定義的動畫名稱用來被animation-name所使用 |
animation-name | 檢索或設置對象所應用的動畫名稱 ,必須與規則@keyframes配合使用,因爲動畫名稱由@keyframes定義 |
animation-duration | 檢索或設置對象動畫的持續時間 |
animation-timing-function | 檢索或設置對象動畫的過渡類型 |
animation-delay | 檢索或設置對象動畫的延遲時間 |
animation-iteration-count | 檢索或設置對象動畫的循環次數 |
animation-direction | 檢索或設置對象動畫在循環中是否反向運動 |
animation-play-state | 檢索或設置對象動畫的狀態 |
animation翻譯成中文是動畫的意思,熟練運用之後你可以用它來做各種各樣炫酷的動畫。
@keyframes:定義一個動畫,定義的動畫名稱用來被animation-name所使用。
div{
width:50px;
height:50px;
background:#f40;
border-radius:50%;
animation:mymove 2s;
}
@keyframes mymove{
0% {width:50px;height:50px;}
50% {width:100px;height:100px;}
100% {width:50px;height:50px;}
}
@keyframes主要是做關鍵幀動畫的,每個@keyframes後面都要跟一個名字,事例中使用了mymove
作爲幀動畫的名字,然後可以在樣式內對關鍵幀添加樣式,然後根據關鍵幀 @keyframes
就能自動形成流暢的動畫了。
animation-name:檢索或設置對象所應用的動畫名稱 ,必須與規則@keyframes配合使用,因爲動畫名稱由@keyframes定義
div{
width:50px;
height:50px;
background:#f40;
border-radius:50%;
animation-name:mymove;
animation-duration:2s;
}
@keyframes mymove{
0% {width:50px;height:50px;}
50% {width:100px;height:100px;}
100% {width:50px;height:50px;}
}
在animation-name
使用之前,我們已經定義了一個名爲mymove
的幀動畫,這裏把幀動畫的名字作爲了animation-name的值,含義是當前元素將執行所設置的幀動畫。
animation-duration:檢索或設置對象動畫的持續時間
繼續看上一個案例,僅僅有幀動畫和需要執行的動畫名稱是不足以形成動畫的,我們還需要設置一個動畫執行所需要的時間,這裏就用到了animation-duration
屬性,所以上一案例所展示的時間爲兩秒鐘執行一次。
animation-timing-function:檢索或設置對象動畫的過渡類型
div{
width:100px;
height:50px;
background:#f40;
position:relative;
animation-name:mymove;
animation-duration:3s;
animation-timing-function:ease-in-out;
}
@keyframes mymove{
0% {left:0px;}
100% {left:300px;}
}
animation-timing-function
的作用就是改變動畫在每一幀的快慢。這裏transition-timing-function
僅展示值爲ease-in-out
的動畫效果,可以理解爲慢-快-慢
。其他的不做展示,可以參考下表進行理解。
值 | 描述 |
---|---|
linear | 動畫從頭到尾的速度是相同的。 |
ease | 默認。動畫以低速開始,然後加快,在結束前變慢。 |
ease-in | 動畫以低速開始。 |
ease-out | 動畫以低速結束。 |
ease-in-out | 動畫以低速開始和結束。 |
cubic-bezier(n,n,n,n) | 在 cubic-bezier 函數中自己的值。可能的值是從 0 到 1 的數值。 |
animation-delay:檢索或設置對象動畫的延遲時間
div{
width:50px;
height:50px;
background:#f40;
border-radius:50%;
animation-name:mymove;
animation-duration:2s;
animation-delay:2s;
}
@keyframes mymove{
0% {width:50px;height:50px;}
50% {width:100px;height:100px;}
100% {width:50px;height:50px;}
}
這裏animation-delay
的值爲2s
,意思是動畫將在延遲兩秒秒後延遲執行。
animation-iteration-count:檢索或設置對象動畫的循環次數
div{
width:50px;
height:50px;
background:#f40;
border-radius:50%;
animation-name:mymove;
animation-duration:2s;
animation-iteration-count:infinite;
}
@keyframes mymove{
0% {width:50px;height:50px;}
50% {width:100px;height:100px;}
100% {width:50px;height:50px;}
}
這裏animation-iteration-count
的值爲infinite
,意思是動畫將會無限次的執行,這也就達到了循環的效果,當然你還可以給它具體的數值,當執行你設置的次數後它會自動停止。
animation-direction:檢索或設置對象動畫在循環中是否反向運動
div{
width:100px;
height:50px;
background:#f40;
position:relative;
animation-name:mymove;
animation-duration:2s;
animation-iteration-count:infinite;
animation-direction:alternate;
}
@keyframes mymove{
0% {left:0px;}
100% {left:300px;}
}
這裏animation-direction
的值爲alternate
,代表動畫將會來回的反覆執行,他還有其它屬性,在下表給出供小夥伴們自己嘗試。
值 | 描述 |
---|---|
normal | 默認值。動畫按正常播放。 |
reverse | 動畫反向播放。 |
alternate | 動畫在奇數次(1、3、5...)正向播放,在偶數次(2、4、6...)反向播放。 |
alternate-reverse | 動畫在奇數次(1、3、5...)反向播放,在偶數次(2、4、6...)正向播放。 |
initial | 設置該屬性爲它的默認值。 |
inherit | 從父元素繼承該屬性。 |
animation-play-state:檢索或設置對象動畫的狀態
<style>
div{
width:50px;
height:50px;
background:#f40;
border-radius:50%;
animation-name:mymove;
animation-duration:2s;
animation-iteration-count:infinite;
}
@keyframes mymove{
0% {width:50px;height:50px;}
50% {width:100px;height:100px;}
100% {width:50px;height:50px;}
}
</style>
<body>
<button onclick="pause()">暫停</button>
<button onclick="run()">恢復</button>
<div></div>
</body>
function pause(){
document.querySelector('div').style.animationPlayState="paused"
}
function run(){
document.querySelector('div').style.animationPlayState="running"
}
animation-play-state
的默認值爲running
,就是動畫執行的意思,在實際應用中我們經常使用js來操作這個屬性,從而控制動畫的播放和暫停。
今天我們一共學習了八個屬性值,他們都是屬於animation
屬性的,這裏給出屬性值在animation
中的簡寫方式。
animation: name duration timing-function delay iteration-count direction play-state;
div{
animation:mymove 2s ease-in-out 3s infinite alternate running;
}
那麼這裏的意思就是mymove
動畫將在三秒鐘後開始,以兩秒一個循環慢-快-慢方式,進行動畫的展示,並且每次動畫過後都會向相反方向執行動畫。
結論
經過以上的學習,相信你已經初步瞭解了animation
的用法,隨着你對animation
的深入理解,是可以做一些有創造性的動畫的,你可以看看自己之前用js寫的各種動畫,嘗試着用我們兩篇文章所學的內容進行修改,相信你一定會對這兩個屬性有更深的理解。
但是現在我們只是學會了過渡和動畫,我們現在還不能對圖形進行一系列的不規則操作,而transform(變形)
就是來操作改變成特殊圖形的,我將在接下來的文章繼續爲你講解translate(移動)
以及transform(變形)
,跟進我的腳步吧,跟我一起在2020年前掌握css動畫!
結語
以上就是本文章的全部內容了,如果有不正確的地方歡迎指正。
感謝您的閱讀,如果感覺有用不妨點贊/轉發。
前端深入系列是一個踩坑系列,是由我本人對工作和學習中所遇到的問題和踩過的坑的一個探索與總結,在此記錄分享出去,同時也是對自我技能的一個反思和追問。
前端路漫,踩坑不斷。
前端深入系列持續更新中……
以上2019-10-21。