以前說的過渡也是屬於動畫的範圍,只不過它只能是開始到過渡這兩個點,中間由瀏覽器去完成,而動畫容許開發者一幀一幀的去編碼。css
要執行的動畫都寫在這個規則裏html
my-css
是自定義的名字瀏覽器
@keyframes my-css{ from {top:0px;} to {top:200px;} }
from就是以前的狀態,to是以後的狀態,嗯,這個其實和過渡沒啥區別,這是第一種寫法。動畫
而後就是這行代碼編碼
animation: my-css 5s;
完整代碼code
<!doctype html> <html> <head> <title></title> <meta charset="utf-8" /> <style type="text/css"> .container{ text-align: center; line-height: 200px; width: 200px; height: 200px; background: skyblue; /*關鍵代碼*/ animation: my-css 5s; } @keyframes my-css{ from {width:200px;} to {width:400px;} } </style> </head> <body> <div class="container">狠人大帝</div> </body> </html>
這只是單純一種屬性的變化,多種屬性的變化是這樣的orm
@keyframes my-css{ from { width:200px; height: 200px; background: skyblue; } to {width:400px; height: 400px; background: pink; } }
接下來是一幀一幀的完成htm
@keyframes my-css{ 0% { top:0px;left: 0px; transform: rotate(0deg); background: skyblue; } 25% { left:200px; top:0px; transform: rotate(45deg); background: pink; } 50% { top:200px; left:200px; transform: rotate(90deg); background: brown; } 75% {top: 200px; left:0px; transform: rotate(135deg); background: #456920; } 100% {top:0px; left:0px; transform: rotate(180deg); background: skyblue; } }
如此動畫的編寫規則就是這樣,接下來看animation
屬性utf-8
它是多個屬性的集合開發
animation: name duration timing-function delay iteration-count direction fill-mode play-state;
以上屬性若是單獨使用應該加上前綴animation-
改變CSS代碼
.container{ text-align: center; line-height: 200px; width: 200px; height: 200px; background: skyblue; position: absolute; /*關鍵代碼*/ animation: my-css 5s 2; animation-fill-mode: forwards; }