css3 animate的小示例
先看一段代码~
<!DOCTYPE>
<html>
<head>
<title>CSS3</title>
</head>
<style type="text/css">
.div{
position: relative;
width: 150px;
height: 150px;
border: 1px solid #354789;
overflow:hidden;
}
.ball{
position: absolute;
bottom: 20px;
width: 20px;
height: 20px;
border-radius: 10px;
background: #444;
}
.ball:hover{
animation: move 1.5s linear infinite alternate;
-moz-animation:move 1.5s linear infinite alternate;
-o-animation:move 1.5s linear infinite alternate;
-webkit-animation: move 1.5s linear infinite alternate;
}
@keyframes move{
0% {background: red;top:150px;opacity: 1.0}
25% {background: yellow;top:100px;opacity: 0.8}
50% {background: blue;top:50px;opacity: 0.6}
75% {background: green;top:0px;opacity: 0.4}
100% {background: red;top:150px;opacity: 1.0}
}
@-moz-keyframes move{
0% {background: red;top:150px;opacity: 1.0}
25% {background: yellow;top:100px;opacity: 0.8}
50% {background: blue;top:50px;opacity: 0.6}
75% {background: green;top:0px;opacity: 0.4}
100% {background: red;top:150px;opacity: 1.0}
}
@-webkit-keyframes move{
0% {background: red;top:150px;opacity: 1.0}
25% {background: yellow;top:100px;opacity: 0.8}
50% {background: blue;top:50px;opacity: 0.6}
75% {background: green;top:0px;opacity: 0.4}
100% {background: red;top:150px;opacity: 1.0}
}
@-o-keyframes move{
0% {background: red;top:150px;opacity: 1.0}
25% {background: yellow;top:100px;opacity: 0.8}
50% {background: blue;top:50px;opacity: 0.6}
75% {background: green;top:0px;opacity: 0.4}
100% {background: red;top:150px;opacity: 1.0}
}
</style>
<body>
<div class="div">
<div class="ball">
</div>
</div>
</body>
</html>
运行结果就是一个小圆匀速向上运动背景色和透明度都在变化
如下图
现在我们来详细介绍每个属性的意思
###animation 属性是一个简写属性,用于设置六个动画属性:
animation-name //动画名称,规定需要绑定到选择器的 keyframe 名称。
animation-duration //请始终规定 animation-duration 属性,否则时长为 0,就不会播放动画了。
animation-timing-function //规定动画的速度曲线。
- linear 动画从头到尾的速度是相同的。
- ease 默认。动画以低速开始,然后加快,在结束前变慢。
- ease-in 动画以低速开始。
- ease-out 动画以低速结束。
- ease-in-out 动画以低速开始和结束。
- cubic-bezier(n,n,n,n) 在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。(这个我也没有尝试过:))
animation-delay //定义动画何时开始,值以秒或毫秒计。
animation-iteration-count//定义动画的播放次数。
- n 定义动画播放次数的数值。
- infinite 规定动画应该无限次播放。
animation-direction
- normal 默认值。动画应该正常播放。
- alternate 动画应该轮流反向播放。