css3動畫原理:css
設定關鍵幀(keyframes),過分時間、過分方式等,css會實現從一個效果過渡到另一個效果的過程css3
使用步驟:web
設定關鍵幀css3動畫
@keyframes myfirst { from {background: red;} to {background: yellow;} } @-moz-keyframes myfirst /* Firefox */ { from {background: red;} to {background: yellow;} } @-webkit-keyframes myfirst /* Safari 和 Chrome */ { from {background: red;} to {background: yellow;} } @-o-keyframes myfirst /* Opera */ { from {background: red;} to {background: yellow;} }
設置animation屬性(將關鍵幀捆綁到某個選擇器),animation屬性至少要規定動畫名稱、過渡時間長度less
.myAnimation { animation: myfirst 5s; -moz-animation: myfirst 5s; /* Firefox */ -webkit-animation: myfirst 5s; /* Safari 和 Chrome */ -o-animation: myfirst 5s; /* Opera */ }
下表列出了關鍵幀和animation所包含的全部屬性動畫
animation: name duration timing-function delay iteration-count direction;
屬性 | 描述 | CSS |
@keyframes | 規定動畫。 | 3 |
animation | 全部動畫屬性的簡寫屬性,除了 animation-play-state 屬性。 | 3 |
animation-name | 規定 @keyframes 動畫的名稱。 | 3 |
animation-duration | 規定動畫完成一個週期所花費的秒或毫秒。默認是 0。 | 3 |
animation-timing-function | 規定動畫的速度曲線。默認是 "ease"。 | 3 |
animation-delay | 規定動畫什麼時候開始。默認是 0。 | 3 |
animation-iteration-count | 規定動畫被播放的次數。默認是 1。 | 3 |
animation-direction | 規定動畫是否在下一週期逆向地播放。默認是 "normal"。 | 3 |
animation-play-state | 規定動畫是否正在運行或暫停。默認是 "running"。 | 3 |
animation-fill-mode | 規定對象動畫時間以外的狀態。 | 3 |
在關鍵幀的設置規則中,from和to能夠表示一個動畫的開始(0%)和結束(100%),還能夠使用百分比表示在動畫進行的某個過程spa
@keyframes myfirst { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} } @-moz-keyframes myfirst /* Firefox */ { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} } @-webkit-keyframes myfirst /* Safari 和 Chrome */ { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} } @-o-keyframes myfirst /* Opera */ { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; left:200px; top:0px;} 50% {background: blue; left:200px; top:200px;} 75% {background: green; left:0px; top:200px;} 100% {background: red; left:0px; top:0px;} }
LESS:.net
使用less語法能夠參照這篇博客ssr