以前说的过渡也是属于动画的范围,只不过它只能是开始到过渡这两个点,中间由浏览器去完成,而动画容许开发者一帧一帧的去编码。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; }