您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

过渡动画和帧动画的区别

上一章我们已经了解了Css Sprite需要的基本CSS语法,那么这一章节我们将带领大家体验一下动画的语法,以及不同动画种类之区别。

动画通常分为两种形式:一种是过渡动画、另一种是帧动画。

之前我们曾经说过,Css Sprite在帧动画这一领域独领风骚,那么接下来我们就来分析一下动画领域里面常见的两种形式:过渡动画与帧动画之区别。

首先我们来看看目前各类网站中最常见的一种动画:过渡动画

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  <title>Animate-过渡动画</title>
  <style>
    /* 清除认样式 */
    * { padding: ; margin: ; }
    
    /* 这段是为了居中,不是重点,看不懂的话可以无视 */
    body {
        height: vh;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .animate {
      width: px;
      height: px;
      
      /* 使用预先定义好的动画,过渡动画 */
      animation: change-color s linear infinite alternate;
    }
    
    /* 定义动画 */
    @keyframes change-color {
      from { background: yellow }
      to { background: green }
    }
  </style>
</head>
<body>
  <div class="animate"></div>
</body>
</html>

运行结果:

可以看到盒子的颜色是从黄色慢慢过渡到绿色,所以叫过渡动画,因为其有过渡的。

再来看看帧动画是什么样的:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  <title>Animate-帧动画</title>
  <style>
    /* 清除认样式 */
    * { padding: ; margin: ; }
    
    /* 这段是为了居中,不是重点,看不懂的话可以无视 */
    body {
        height: vh;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .animate {
      width: px;
      height: px;

	  /* 使用预先定义好的动画,帧动画 */
      animation: change-color s steps() infinite alternate;
    }

	/* 定义动画 */
    @keyframes change-color {
      from { background: yellow }
      to { background: green }
    }
  </style>
</head>
<body>
  <div class="animate"></div>
</body>
</html>

运行结果:

可以看到是一帧帧播放的,帧数低的时候有种卡卡的感觉,好像一下一下的分步骤从黄色变成绿色的。那我们把帧数提高一下不就看不到一卡一卡的感觉了吗?来试试看:

<!DOCTYPE html>
<html lang="en">
<head>
  < charset="UTF-8">
  <title>Animate</title>
  <style>
    /* 清除认样式 */
    * { padding: ; margin: ; }
    
    /* 这段是为了居中,不是重点,看不懂的话可以无视 */
    body {
        height: vh;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .animate {
      width: px;
      height: px;
      
      /* 使用预先定义好的动画 */
      animation: change-color s steps() infinite alternate;
    }
    
    /* 定义动画 */
    @keyframes change-color {
      from { background: yellow }
      to { background: green }
    }
  </style>
</head>
<body>
  <div class="animate"></div>
</body>
</html>

运行结果:

虽然一样了,但是怎么感觉更麻烦了呢?还要自己去指定帧数,而过渡动画都是全的,帧动画是不是不如过渡动画呢?实际上并不是这样的,帧动画有着自己的适用场景。接下来我们就来探讨一下何时适合帧动画,何时又适合过渡动画。

乍一看好像过渡动画更胜一筹,但实际上他们两个各自有各自的适用场景。

下一章我们就来看看什么样的场景适合过渡动画。


联系我
置顶