CSS中的动画

526 阅读2分钟

CSS中动画的类型

  1. transition补间动画:从一个状态切换到另一个状态
  2. keyframe关键帧动画:可以设置多个关键帧
  3. 逐帧动画:逐帧切换

补间动画:transition

  1. transition-timing-function: 过渡函数
  • ease
  • linear
  • ease-in
  • ease-out
  • ease-in-out
  • 还可以自定义bezier曲线函数
  1. transition-property:过渡属性
  • 当transition-property的值为all的时候,表示所有可以使用过渡动画的属性
  1. transition-delay:开始出现过渡的延迟时间
  2. transition-duration:过渡时间

当动画属性通过transion属性合并表示的时候,第一个时间表示transition-duration,第二个时间表示transition-delay

例如:transition: background 0.8s ease-in 0.3;表示动画0.3s后触发,持续0.8s

<style type="text/css" media="screen">
.transiton{
  margin-top: 50px;
  width: 100px;
  height: 100px;
  background: red;
  transition: 1s background ease;
}
.transiton:hover{
  background: green;
}
</style>
<div class="transiton">
  补间动画
</div>

关键帧动画:keyframes和animation的组合使用

  1. animation-name:
  2. animaion-duration:
  3. animation-timing-function:
  4. animation-delay:
  5. animation-iteration-count:设置动画播放的次数,infinite表示无线循环
  6. animation-direction:设置动画的播放方向。normal表示每次播放都是向前播放。alternate表示第偶数次向前播放,第基数次向
  7. animation-play-state:动画的播放状态 runing表示播放,paused表示暂停
  • 以上四个属性可以同时在animation中设置:
   animation: 0.1s run ease 0.8;
<style type="text/css" media="screen">
.keyframetest{
  width: 100px;
  height: 100px;
  background: red;
  animation: 0.1s run ease 0.8;
}
@keyframes run{ //run就是animation-name的值
 0%/from{
   width: 100px;
 }
 10%{
   width: 50px;
 }
 50%{
   width: 200px;
 }
 100%/to{
   width: 800px;
 }
}
</style>

<div class='keyframetest'>
  关键帧动画
</div>
  • 自定义关键帧动画中的0%可以用from代替,100%可以用to代替

补间动画和关键帧动画的区别

  1. 补间动画需要一个动作触发,不如鼠标悬浮;
  2. 关键帧动画不需要,一开始就会进入动画。

逐帧动画

在关键帧动画的基础上做加工

  1. animation-timing-function:steps(1) //每个关键帧之间没有补间

线性变换:transform

1.旋转:rotate(Xdeg)

<style type="text/css" media="screen">
  .wrapper{
    width: 100px;
    height: 100px;
    border: 5px dotted red;
  }
  .wrapper div{
	width: 100px;
	height: 100px;
	background: yellow;
	transform: rotate(45deg);
	-webkit-transform: rotate(45deg);
	line-height: 100px;
	text-align: center;
  }
  .wrapper div span{
	display: block;
	transform: rotate(-45deg);
	-webkit-transform: rotate(-45deg);
   }
</style>
<div class="rotate">
  <div>
    <span>我不要旋转</span>
  </div>	
</div>

  1. 扭曲:skew(Xdeg,Ydeg)
<style type="text/css" media="screen">
 .skew{
    margin-top: 50px;
    width: 100px;
    height: 100px;
    border: 5px dotted red;
 }
 .skew div{
    width: 100px;
    height: 100px;
    background: yellow;
    transform: skew(45deg);
    -webkit-transform: skew(45deg);
    line-height: 100px;
    text-align: center;
 }
 .skew div span{
    display: block;
    transform: skew(-45deg);
    -webkit-transform: skew(-45deg);
 }
</style>
<div class="skew">
  <div>
    <span>我不要扭曲</span>
  </div>	
</div>

  1. 水平和垂直方向上缩放scale(X,Y)
<style type="text/css" media="screen">
 .scale{
    margin-top: 50px;
    width: 100px;
    height: 100px;
    border: 5px dotted red;
 }
 .scale div{
    width: 100px;
    height: 100px;
    background: yellow;
    transform: scale(0.5,0.5);
    -webkit-transform: scale(0.5,0.5);
    line-height: 100px;
    text-align: center;
 }
</style>
<div class="scale">
  <div>
  <span>我将缩小0.5倍</span>
  </div>	
</div>

  1. 位移:translate(Xpx,Ypx)
<style type="text/css" media="screen">
 .translate{
    margin-top: 50px;
    width: 100px;
    height: 100px;
    border: 5px dotted red;
 }
 .translate div{
    width: 100px;
    height: 100px;
    background: yellow;
    transform: translate(10px,10px);
    -webkit-transform: translate(10px,10px);
    line-height: 100px;
    text-align: center;
 }
</style>
<div class="translate">
  <div>
  <span>我将缩小0.5倍</span>
  </div>	
</div>

面试题

CSS动画的实现方式有几种

  1. 过渡动画或者叫补间动画
  2. 关键帧动画

过渡动画和关键帧动画的区别

  1. 过渡动画需要有状态的变化
  2. 关键帧动画不需要状态的变化
  3. 关键帧动画能控制的更精细

如何实现逐帧动画

  1. 去掉补间过程:animation-timing-function:setps(1)

CSS动画的性能

  1. 性能不坏
  2. 部分情况下是优于js
  3. 但是JS可以做到更好
  4. 部分高危属性比如:box-shadow无论是js还是CSS实现起来性能都不是很好