美文网首页前端CSS专题
css 动画的3种方式

css 动画的3种方式

作者: 黑木令 | 来源:发表于2021-03-22 22:46 被阅读0次

1. css 动画的三种方式

   1. transition 过渡
   2. transform 变形
   3. animation 关键帧动画


1. transition 过渡

   1. 语法:

      1. transition: property duration timing-function delay

         1. transition: 属性是个复合属性
         2. transition-property: 规定设置过渡效果的 css 属性名称
         3. transition-duration: 规定完成过渡效果需要多少秒或毫秒
         4. transition-timing-function: 指定过渡函数, 规定速度效果的速度曲线
         5. transition-delay: 指定开始出现的延迟时间

      2. 默认值分别为: all 0 ease 0;

         1. 注: transition-duration 时长为 0, 不会产生过渡效果

      3. 改变多个 css 属性的过渡效果时, 代码示例:

         1. a {
               transition: background 0.8s ease-in 0.3s, color 0.6s ease-out 0.3s;
            }

      4. 子属性:

            1. transition-property

                 1. transition-property: none |all |property;
                 2. 值为 none 时, 没有属性会获得过渡效果
                 3. 值为 all 时, 所有属性都将获得过渡效果
                 4. 值为指定的 css 属性应用过渡效果, 多个属性用逗号隔开
             2. color : background-color, border-color, color, outline-color ;
             3. length : 真实的数字 如:word-spacing,width,vertical-align,top,right,bottom,left,padding,outline-width,margin,min-width,min-height,max-width,max-height,line-height,height,border-width,border-spacing,
             4. integer : 离散步骤(整个数字), 在真实的数字空间, 以及使用 floor()转换为整数时发生 如: outline-offset,z-index
             5. number : 真实的(浮点型)数值, 如:zoom,opacity,font-weight
             6. rectangle : 通过x, y, width 和 height(转为数值)变换,如: crop
             7. visibility : 离散步骤,在0到1数字范围之内,0表示“隐藏”,1表示完全“显示”,如:visibility
             8. shadow : 作用于color, x, y 和 blur(模糊)属性,如:text-shadow
             9. background-image : 通过每次停止时的位置和颜色进行变化。它们必须有相同的类型(放射状的或是线性的)和相同的停止数值以便执行动画。

           2. transition-duration

               1. transition-duration: time;
                2. 该属性主要用来设置一个属性过渡到另一个属性所需的时间, 也就是从旧属性过渡到新属性花费的时间长度, 俗称持续时间

            3. transition-timing-function

                1. transition-timing-function: linear| ease| ease-in| ease-out| ease-in-out| cubic-bezier(n,n,n,n);
                2. 该属性指的是过渡的 “缓动函数” 。 主要用来指定浏览器的过渡速度, 以及过渡期间的操作进展情况, 解释下:
                3. 注意: 值 cubic-bezier(n,n,n,n) 可以定义自己的值, 如 cubic-bezier(0.42,0,0.58,1)

            4. transition-delay

                1. 这个属性没什么说的了, 就是过渡效果开始前的延迟时间, 单位秒或者毫秒


2. transform 变形

   1. 可以利用 transform 功能来实现文字或图像的 旋转、缩放、倾斜、移动 这四种类型的变形处理

      1. 旋转 rotate

         1. 用法: transform: rotate(45deg);
         2. 共一个参数 “角度”, 单位 deg 为度的意思, 正数为顺时针旋转, 负数为逆时针旋转, 上述代码作用是顺时针旋转45度

      2. 缩放 scale

         1. 用法: transform: scale(0.5) 或者 transform: scale(0.5, 2);
         2. 一个参数时: 表示水平和垂直同时缩放该倍率
         3. 两个参数时: 第一个参数指定水平方向的缩放倍率, 第二个参数指定垂直方向的缩放倍率 。

      3. 倾斜 skew

         1. 用法: transform: skew(30deg) 或者 transform: skew(30deg, 30deg);
         2. 一个参数时: 表示水平方向的倾斜角度 。
         3. 两个参数时: 第一个参数表示水平方向的倾斜角度, 第二个参数表示垂直方向的倾斜角度 。
         4. skew 的默认原点 transform-origin 是这个物件的中心点

      4. 移动 translate

         1. 用法: transform: translate(45px) 或者 transform: translate(45px, 150px);
         2. 一个参数时: 表示水平方向的移动距离;
         3. 两个参数时: 第一个参数表示水平方向的移动距离, 第二个参数表示垂直方向的移动距离 。

   2. 基准点 transform-origin

      1. 在使用 transform 方法进行文字或图像的变形时, 是以元素的中心点为基准点进行的 。 使用 transform-origin 属性, 可以改变变形的基准点 。
      2. 用法: transform-origin: 10px 10px;
      3. 表示相对左上角原点的距离, 单位 px, 第一个参数表示相对左上角原点水平方向的距离, 第二个参数表示相对左上角原点垂直方向的距离;
      4. 两个参数除了可以设置为具体的像素值, 其中第一个参数可以指定为 left、center、right, 第二个参数可以指定为 top、center、bottom。

   3. 多方法组合变形

      1. 用法: transform: rotate(45deg) scale(0.5) skew(30deg, 30deg) translate(100px, 100px);
      2. 这四种变形方法顺序可以随意, 但不同的顺序导致变形结果不同, 原因是变形的顺序是从左到右依次进行


3. animation 关键帧动画

   1. 在 CSS3 中创建动画, 您需要学习 @keyframes 规则 。
   2. @keyframes 规则用于创建动画 。 在 @keyframes 中规定某项 CSS 样式, 就能创建由当前样式逐渐改为新样式的动画效果 。
   3. 必须定义动画的名称和时长 。 如果忽略时长, 则动画不会允许, 因为默认值是 0。
   4. 请用百分比来规定变化发生的时间, 或用关键词 "from" 和 "to", 等同于 0% 和 100% 。

   5. 语法

      1. animation: name duration timing-function delay iteration-count direction;
      1. animation-name 规定需要绑定到选择器的 keyframe 名称
      2. animation-duration 规定动画完成一个周期所花费的秒或毫秒。默认是 0。
      3. animation-timing-function 规定动画的速度曲线。 默认是 "ease"。
      4. animation-delay 规定动画何时开始 。 默认是 0。
      5. animation-iteration-count 规定动画被播放的次数 。 默认是 1。
      6. animation-direction 规定动画是否在下一周期逆向地播放 。 默认是 "normal"; alternate (轮流),。

1. alternate (轮流): 动画播放在第偶数次向前播放, 第奇数次向反方向播放 (animation-iteration-count 取值大于1时设置有效
   2. 语法: animation-direction: alternate;

2. animation-play-state 规定动画是否正在运行或暂停 。 默认是 "running" 播放; paused 暂停播放 。
   1. 语法: animation-play-state: paused;

3. animation-fill-mode 属性规定动画在播放之前或之后, 其动画效果是否可见; 规定对象动画时间之外的状态; none | forwards | backwards | both 。
   1. none: 不改变默认行为 (默认, 回到动画没开始时的状态) 。
   2. forwards: 当动画完成后,保持最后一个属性值(在最后一个关键帧中定义) (动画结束后动画停留在结束状态) 。
   3. backwards: 在 animation-delay 所指定的一段时间内, 在动画显示之前, 应用开始属性值 (在第一个关键帧中定义) (动画回到第一帧的状态)。
   4. both: 向前和向后填充模式都被应用 (根据 animation-direction 轮流应用 forwards 和 backwords 规则)。
   5. 语法: animation-fill-mode: forwards
      1. 0% 是动画的开始, 100% 是动画的完成。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>transition过渡/transform变形/animation关键帧动画</title>
</head>
<style>
  .w_publuc-style {
    margin-bottom: 30px;
    border: 3px solid #ccc;
  }
  /* transition 过渡 */
  .w_transition {
    width: 200px;
    height: 200px;
    background-color: wheat;
    color: #333;

    transition: background 0.8s ease-in 0.3s, width 0.8s ease-in 0.3s, color 0.6s ease-out 0.3s;
    -moz-transition: background 0.8s ease-in 0.3s, width 0.8s ease-in 0.3s, color 0.6s ease-out 0.3s; /* Firefox 4 */
    -webkit-transition: background 0.8s ease-in 0.3s, width 0.8s ease-in 0.3s, color 0.6s ease-out 0.3s; /* Safari and Chrome */
    -o-transition: background 0.8s ease-in 0.3s, width 0.8s ease-in 0.3s, color 0.6s ease-out 0.3s; /* Opera */
  }
  .w_transition:hover {
    background: yellow;
    width: 300px;
    color: blueviolet;
  }
  /* transform 变形 */
  .w_transform {
    margin:30px;
    width:200px;
    height:100px;
    background-color:yellow;

    /* Rotate div */
    transform:rotate(9deg);
    -ms-transform:rotate(9deg); /* Internet Explorer */
    -moz-transform:rotate(9deg); /* Firefox */
    -webkit-transform:rotate(9deg); /* Safari 和 Chrome */
    -o-transform:rotate(9deg); /* Opera */
  }

  /* animation 关键帧动画 */
  .w_animation {
    width: 200px;
    height: 200px;
    background: red;
    animation: myfirst 2s ease 0.5s infinite alternate;

    /* Firefox */
    -moz-animation:myfirst 2s ease 0.5s infinite alternate;
    /* Safari and Chrome */
    -webkit-animation:myfirst 2s ease 0.5s infinite alternate;
    /* Opera */
    -o-animation:myfirst 2s ease 0.5s infinite alternate;
  }
  @keyframes myfirst {
    from {background:red;}
    to {background:yellow;}
  }
  /* Firefox */
  @-moz-keyframes myfirst {
    from {background:red;}
    to {background:yellow;}
  }
  /* Safari and Chrome */
  @-webkit-keyframes myfirst {
    from {background:red;}
    to {background:yellow;}
  }
  /* Opera */
  @-o-keyframes myfirst {
    from {background:red;}
    to {background:yellow;}
  }
</style>
<body>
  <div class="w_transition w_publuc-style">transition 过渡</div>
  <div class="w_transform w_publuc-style">transform 变形</div>
  <div class="w_animation w_publuc-style">animation 关键帧动画</div>
</body>
</html>
111.gif
希望对大家有所帮助,如有问题还望不吝赐教,本人会及时更改;如果大家喜欢可以点个关注(如需转载,请注明出处)。

相关文章

  • CSS动画

    1.css动画实现的几种方式transitionkeyframes(animation)2.过渡动画和关键帧动画的...

  • 2018-11-27第九天总结

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • 2019-01-02 css3过渡动画 css3圆角阴影透明度

    一、 动画网页动画可以通过以下几种方式实现(gif、flash 除外),css3 动画SVG 动画JS 动画(包括...

  • 开发笔记3-H5页面实现帧动画

    动画的实现方式有以下几种方式:js、css、canvas、gif gif对透明效果支持不好,比较方便,但是现在动画...

  • requestAnimationFrame 请求动画帧 一定懂

    requestAnimationFrame 在网页开发中实现动画实现动画的方式有下面几种。css中的 transi...

  • 动画使用库,总结

    导航过渡、动画和转换(2D/3D)【relative】、渐变html元素隐藏的几种方式与动画 一、用到的css动画...

  • 五、Vue动画 ------ 2020-05-07

    1、常见的能触发动画的操作及添加动画的方式 2、Vue动画的基本使用:通过添加CSS样式使用 3、Vue动画的基本...

  • requestAnimationFrame

    动画 web开发中实现动画的方式有多种,CSS3中的transition和animation,js中的setInt...

  • css3实现动画有几种方式?

    这是一个考验面试者对css的基础知识。 css实现动画主要有3种方式,第一种是:transition实现渐变动画,...

  • Css动画 小球横移

    学习css 动画,建立小球左右横移的动画原料:html5 Css3html CSS css 动画属性介绍 其中an...

网友评论

    本文标题:css 动画的3种方式

    本文链接:https://www.haomeiwen.com/subject/ynpthltx.html