经过 @keyframes 规则,您可以建立动画。 css
建立动画的原理是,将一套 CSS 样式逐渐变化为另外一套样式。 html
在动画过程当中,您可以屡次改变这套 CSS 样式。 css3
以百分比来规定改变发生的时间,或者经过关键词 "from" 和 "to",等价于 0% 和 100%。 web
0% 是动画的开始时间,100% 动画的结束时间。 浏览器
为了得到最佳的浏览器支持,您应该始终定义 0% 和 100% 选择器。 动画
注释:请使用动画属性来控制动画的外观,同时将动画与选择器绑定。 spa
@keyframes animationname {keyframes-selector {css-styles;}}
<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; background:red; position:relative; animation:mymove 5s infinite; -moz-animation:mymove 5s infinite; /* Firefox */ -webkit-animation:mymove 5s infinite; /* Safari and Chrome */ -o-animation:mymove 5s infinite; /* Opera */ } @keyframes mymove { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-moz-keyframes mymove /* Firefox */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-webkit-keyframes mymove /* Safari and Chrome */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } @-o-keyframes mymove /* Opera */ { 0% {top:0px; background:red; width:100px;} 100% {top:200px; background:yellow; width:300px;} } </style> </head> <body> <p><b>注释:</b>本例在 Internet Explorer 中无效。</p> <div></div> </body> </html>CSS3的animation相似于transition属性,他们都是随着时间改变元素的属性值。他们主要区别是transition须要触发一个事件 (hover事件或click事件等)才会随时间改变其css属性;而animation在不须要触发任何事件的状况下也能够显式的随着时间变化来改变元 素css的属性值,从而达到一种动画的效果。这样咱们就能够直接在一个元素中调用animation的动画属性,基于这一点,css3的 animation就须要明确的动画属性值,咱们须要keyframes来定义不一样时间的css属性值,达到元素在不一样时间 段变化的效果。
给一个元素调用animation属性 3d
.demo1 { width: 50px; height: 50px; margin-left: 100px; background: blue; -webkit-animation-name:'wobble';/*动画属性名,也就是咱们前面keyframes定义的动画名*/ -webkit-animation-duration: 10s;/*动画持续时间*/ -webkit-animation-timing-function: ease-in-out; /*动画频率,和transition-timing-function是同样的*/ -webkit-animation-delay: 2s;/*动画延迟时间*/ -webkit-animation-iteration-count: 10;/*定义循环资料,infinite为无限次*/ -webkit-animation-direction: alternate;/*定义动画方式*/ }
=========================== code
自用: orm
/*定义旋转*/
@-webkit-keyframes rock{ 0%{ transform:rotate(0deg) } 10%{ transform:rotate(3deg) } 20%{ transform:rotate(-3deg) } 30%{ transform:rotate(2deg) } 40%{ transform:rotate(-2deg) } 50%{ transform:rotate(1deg) } 60%{ transform:rotate(-1deg) } 70%{ transform:rotate(0deg) } 100%{ transform:rotate(0deg) } }
/**给须要抖动选中的元素加上动画*/
.c_zongzi_box_rock{ -webkit-animation:rock 2s infinite; }
infinite是无限的动画的意思 rock定义的-webkit-keyframes的名字,2s是动画的时间
transform-origin是动画变换的基点(参照点)
一、top left | left top 等价于 0 0 | 0% 0%
二、top | top center | center top 等价于 50% 0
三、right top | top right 等价于 100% 0
四、left | left center | center left 等价于 0 50% | 0% 50%
五、center | center center 等价于 50% 50%(默认值)
六、right | right center | center right 等价于 100% 50%
七、bottom left | left bottom 等价于 0 100% | 0% 100%
八、bottom | bottom center | center bottom 等价于 50% 100%
九、bottom right | right bottom 等价于 100% 100%
其中 left,center right是水平方向取值,对应的百分值为left=0%;center=50%;right=100%而top center bottom是垂直方向的取值,其中top=0%;center=50%;bottom=100%;若是只取一个值,表示垂直方向值不变