过渡和动画

2017-07-17  本文已影响0人  卓小生

transition(过渡)

在CSS3引入transition之前css没有时间轴,所有的状态变化都是瞬间完成

div{
    height:15px;
    width:15px;
}

div:hover{
    height: 450px;
    width: 450px;
}

transition的作用在于,指定状态变化所需要的时间

transition: 1s;

指定属性

我们还可以指定transition适用的属性,比如只适用于height

transition: 1s height;

这样一来,只有height的变化需要1秒实现,其他变化(主要是width)依然瞬间实现,在同一行transition语句中,可以分别指定多个属性

transition: 1s height, 1s width;

delay(延时)

我们还可以指定变化的延时开始,比如这个地方我们希望让height先发生变化,等结束以后,再让width发生变化, 我们只需要为width指定一个delay参数

transition: 1s height, 1s 1s width;

上面代码指定,width在1秒之后,再开始变化,也就是延迟(delay)1秒

delay的真正意义在于,它指定了动画发生的顺序,使得多个不同的transition可以连在一起,形成复杂效果

transition-timing-function

transition的状态变化速度(又称timing function),默认不是匀速的,而是逐渐放慢,这叫做ease
除了ease以外,其他模式还包括

贝塞尔函数工具

语法

transition: 1s 1s height ease;

这其实是一个简写形式,可以单独定义成各个属性

transition-property: height;
transition-duration: 1s;
transition-delay: 1s;
transition-timing-function: ease;

注意事项

事件

transitionend

div{
  transition: height 1s, width 1s;
}
div:hover{
  width: 100px;
  height: 100px;
}

一次hover会触发两次transitionend事件

animation(动画)

transition 比较简单,animation可以帮我们实现复杂的动画

基本用法
@keyframes change-color{
  0% { 
    background: red;
  }
  50%{
    background: blue;
  }
  100%{
    background: orange;
  }
}
div{
  height: 100px;
  width: 200px;
  border: 1px solid #111;
 
}
div:hover{
    animation-name: change-color;
    animation-duration: 2s;
}

我们使用keyframes(关键帧)来定义一个动画效果, change-color是我们取得动画名字,每个百分比后面写的是相应时间点我关键帧样式,
定义好后,在animation(动画)属性中调用,2s 表示的动画的持续时间

指定播放次数(animation-iteration-count)

默认情况下,动画只会播放一次, 我们可以指定动画具体播放的次数,比如3次:

div:hover {
    animation-name: change-color;
    animation-duration: 2s;
    animation-iteration-count: 3;
}

也可以无线循环播放:

div:hover {
    animation-name: change-color;
    animation-duration: 2s;
    animation-iteration-count: infinite;
}

心跳demo

延时(animation-delay)

animation-timing-function

动画播放前后的状态(animation-fill-mode)

animation-fill-mode: none | backwards | forwards| both;

动画播放的方向(animation-direction)

动画连续播放时,每次都是从结束状态跳回到起始状态,再开始播放。animation-direction属性,可以改变这种行为

animation-direction可以使用下列值:

下图解释了它的规律(假定动画连续播放三次)


简单说,animation-direction指定了动画播放的方向,最常用的值是normal和reverse。浏览器对其他值的支持情况不佳,应该慎用

语法

div:hover {
  animation-name: change-color;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-delay: 1s;
  animation-fill-mode:forwards;
  animation-direction: normal;
  animation-iteration-count: 3;
}

简写


  animation: animation-name animation-duration animation-timing-function animation-delay animation-iteration-count animation-direction  animation-fill-mode;

例子:

div:hover {
  animation: 1s 1s change-color linear 3 forwards normal;
}

keyframes关键字用来定义动画的各个状态,它的写法相当自由

@keyframes change-color {
  0% { background: #c00 }
  50% { background: orange }
  100% { background: yellowgreen }
}

0%可以用from代表,100%可以用to代表,因此上面的代码等同于下面的形式

@keyframes change-color {
  from { background: #c00 }
  50% { background: orange }
  to { background: yellowgreen }
}

如果省略某个状态,浏览器会自动推算中间状态,所以下面都是合法的写法。

@keyframes change-color {
  50% { background: orange }
  to { background: yellowgreen }
}

@keyframes change-color {
  to { background: yellowgreen }
}

甚至,可以把多个状态写在一行。

div:hover {
  animation: 1s change-color infinite steps(10);
}

另外一点需要注意的是,浏览器从一个状态向另一个状态过渡,是平滑过渡。steps函数可以实现分步过渡。

div:hover {
  animation: 1s change-color infinite steps(10);
}

文字输入效果demo

animation-play-state

有时,动画播放过程中,会突然停止。这时,默认行为是跳回到动画的开始状态,如果想让动画保持突然终止时的状态,就要使用animation-play-state属性。

div {
    animation: spin 1s linear infinite;
    animation-play-state: paused;
}

div:hover {
  animation-play-state: running;
}

工具

CSS3 Tool

上一篇下一篇

猜你喜欢

热点阅读