当前位置: 代码网 > it编程>网页制作>Css > css动画效果(关键帧)

css动画效果(关键帧)

2024年08月01日 Css 我要评论
基本动画效果,基本参数和属性

目录

一、使用方式

1、keyframes

2、关键帧

二、常用属性

1、基本属性

2、新增属性

三、代码

1、动画效果

2、关键帧


一、使用方式

1、keyframes

(1)通过在style标签中创建keyframes的方式创建动画效果,keyframes后跟自定义名字,如下代码

@keyframes test {
        from {
          margin-left: 0;
          background-color: orange;
        }
        to {
          margin-left: 700px;
          background-color: red;
        }
}

2、关键帧

(1)动画开始,用from,里面包含动画的初始状态

(2)动画结束,用to,里面包含动画的结束状态

(3)可以通过百分比的方式添加动画的中间状态,比如33%等

二、常用属性

1、基本属性

与过渡效果相同,只需要替换名称,参考本文

2、新增属性

(1)重复次数(animation-iteration-count)

--可设置数值

--也可设置为无限次,infinite

(2)运动方向(animation-direction)

--normal 是正常运动

--reverse 反向运动

--alternate 运动完后,动画返回

--alternate-reverse 反向运动完后,动画返回

(3)动画执行状态(animation-play-state)

--running正在执行

--paused暂停

(4)动画填充模式(animation-fill-mode)

--none 默认值,动画执行完毕后回到初始位置

--forwards 动画执行完毕后停止在结束位置

--backwards 动画执行时,在延迟状态时就会转变为开始状态,而仅仅延迟时,只有运行开始时,才会变为开始状态

三、代码

1、动画效果

<!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>动画效果</title>
    <style>
      @keyframes test {
        from {
          margin-left: 0;
          background-color: orange;
        }
        to {
          margin-left: 700px;
          background-color: red;
        }
      }
      .box1 {
        width: 800px;
        height: 800px;
        background-color: silver;
      }
      .box2 {
        background-color: #bfa;
        width: 100px;
        height: 100px;
        animation-name: test;
        animation-duration: 2s;
        /* 重复次数,可设置数值,也可设置为无限次 */
        animation-iteration-count: infinite;
        /* 运动方向
            normal 是正常运动
            reverse 反向运动
            alternate 运动完后,动画返回
            alternate-reverse 反向运动完后,动画返回
            */
        animation-direction: alternate-reverse;
        /* 
            动画执行状态
            running正在执行
            paused暂停
            */
        animation-play-state: running;

        /* 
            动画填充模式
            none 默认值,动画执行完毕后回到初始位置
            forwards 动画执行完毕后停止在结束位置
            backwards 动画执行时,在延迟状态时就会转变为开始状态,而仅仅延迟时,只有运行开始时,才会变为开始状态
            */
        animation-fill-mode: none;
      }
      .box3 {
        background-color: #bfa;
        margin-top: 200px;
        width: 100px;
        height: 100px;
        animation-name: test;
        animation-duration: 2s;
      }
      .box1:hover .box2 {
        animation-play-state: paused;
      }
    </style>
  </head>
  <body>
    <div class="box1">
      <div class="box2"></div>
      <div class="box3"></div>
    </div>
  </body>
</html>

2、关键帧

<!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>关键帧</title>
    <style>
      @keyframes ball {
        from {
          margin-top: 0px;
        }
        33% {
          margin-top: 400px;
        }
        66% {
          margin-top: 300px;
        }
        to {
          margin-top: 400px;
        }
      }
      .box {
        background-color: silver;
        width: 500px;
        height: 500px;
        border-bottom: solid 10px black;
        overflow: hidden;
      }
      .box div {
        border-radius: 50%;
        width: 100px;
        height: 100px;
        float: left;
        animation-name: ball;
        animation-duration: 1s;
        animation-iteration-count: infinite;
        animation-fill-mode: backwards;
        animation-direction: alternate;
        animation-timing-function: ease-in-out;
      }
      .box1 {
        background-color: red;
        animation-delay: 0.1s;
      }
      .box2 {
        background-color: blue;
        animation-delay: 0.2s;
      }
      .box3 {
        animation-delay: 0.3s;
        background-color: orange;
      }
      .box4 {
        animation-delay: 0.4s;
        background-color: yellow;
      }
      .box5 {
        animation-delay: 0.5s;
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="box1"></div>
      <div class="box2"></div>
      <div class="box3"></div>
      <div class="box4"></div>
      <div class="box5"></div>
      <div class="box6"></div>
    </div>
  </body>
</html>

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com