CSS动画animation关键帧的使用

常用动画属性:

animation-name: hd;	设置动画名称(必写)
  
animation-duration: 2s;	设置动画时间(必写)

animation-fill-mode: forwards;     填充模式:停止到最后一帧,保留0帧时的状态
animation-fill-mode: both;     填充模式:停止到最后一帧,不保留0帧时的状态

animation-delay: 2s;     动画延迟2s后执行

animation-iteration-count: infinite;    设置无限循环动画,也可写数字1234等等,表示动画执行1234次

animation-direction: reverse;    动画执行方向,从100%到0%执行。
animation-direction: alternate;	   动画执行方向,从0%到100%执行再从100%到0%执行。

animation-timing-function: ease;   动画执行速度,先快后慢
animation-timing-function: ease-in;   动画执行速度,先慢后快
animation-timing-function: ease-out;   动画执行速度,匀速
animation-timing-function: ease-in-out; 	动画执行速度,两边慢,中间快
animation-timing-function: linear; 	动画执行速度,渐变
animation-timing-function: steps(4,start);	动画执行速度,4步走完。像俄罗斯方块下降速度。从第二步开始
animation-timing-function: steps(4,end);	动画执行速度,4步走完。像俄罗斯方块下降速度。从第一步开始

div:hover{
	animation-play-state:paused;   鼠标放上去动画暂停
	animation-play-state:running;   鼠标放上去动画开始
}

//组合模式(名称,时间,渐变速度,延迟,循环次数,方向,填充模式)
animation: name duration timing-function delay iteration-count direction fill-mode;

举例荧光字

 <div class="word">abc</div>
.word {
        animation: animate linear 2000ms infinite;
    }
 @keyframes animate {
        0% {
            opacity: 0.3;
        }
        100% {
            opacity: 1;
            text-shadow: 0 0 80px Red, 0 0 30px orange, 0 0 6px DarkRed;
        }
    }

动画延迟属性

 .word {
        animation-delay: 900ms;
    }

干货1:简单的动画

1、先声明关键帧名称:hd。
2、在main元素上添加animation-name: hd;
3、设置动画时间animation-duration: 2s;
三步即可完成简单的渐变动画

注:如果0%和100%不写,则默认是初始状态。

<style>
  body {
    width: 100vw;
    height: 100vh;
    background-color: rgb(21, 6, 77);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  main {
    width: 100px;
    height: 100px;
    background-color: #fff;
    /* 设置动画名称(必写) */
    animation-name: hd;
    /* 设置动画时间(必写) */
    animation-duration: 2s;
  }

  /* 定义关键帧hd */
  @keyframes hd {
    /* 起始也可以写from */
    0% {
      background-color: #fff;
    }
    /* 结束也可以写to */
    100% {
      background-color: red;
    }
       /* 如果有共同属性也可以这样写 */
       0%,100%{
		border:1px solid yellow;
	}
  }
</style>
<body>
  <main></main>
</body>

干货2:多个动画关键帧一起绑定

<style>
  body {
    width: 100vw;
    height: 100vh;
    background-color: rgb(21, 6, 77);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  main {
    width: 400px;
    height: 400px;
    border: 1px solid #ddd;
  }

  div {
    width: 100px;
    height: 100px;
    background-color: yellow;
      /* 多个关键帧以逗号隔开 */
    animation-name: hd,bd;
      /* 可以分开定义时间,如果不定义,则执行第一个动画时间 */
    animation-duration: 2s,1s;
  }

  /* 定义关键帧hd */
  @keyframes hd {
    25% {
      transform: translateX(300px);
    }

    50% {
      transform: translate(300px, 300px);
    }

    75% {
      transform: translateY(300px);
    }
  }

  @keyframes bd {
    25% {
     background-color: red;
    }

    50% {
      background-color: yellow;
    }

    75% {
      background-color: blue;
    }
  }
</style>

案例1

<style>
  main {
    width: 100vw;
    height: 100vh;
    background-color: rgb(21, 6, 77);
    transform: scale(0);
    animation-name: hd;
    animation-duration: 2s;
    /* 填充模式:停止到最后一帧 */
    animation-fill-mode: forwards;
     /* 设置无限循环动画 */
    animation-iteration-count: infinite;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  main::after {
    content: "www.baidu.com";
    font-size: 2em;
    color: #fff;
    opacity: 0;
    animation-name: opacity;
    animation-duration: 2s;
    /* 动画延迟2s后执行 */
    animation-delay: 2s;
    animation-fill-mode: forwards;
  }

  /* 定义关键帧hd */
  @keyframes hd {
    25% {
      transform: scale(0.5);
    }

    50% {
      transform: scale(1);
    }

    75% {
      transform: scale(0.5);
    }

    100% {
      transform: scale(1);
    }
  }

  @keyframes opacity {
    to {
      opacity: 0.8;
    }
  }
</style>

案例2:心动动画

在这里插入图片描述

<style>
  body {
    width: 100vw;
    height: 100vh;
    background-color: #34495e;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .heart {
    width: 200px;
    height: 200px;
    background-color: red;
    transform: rotate(45deg);
    animation: hd;
    animation-duration: 1s;
    animation-iteration-count: infinite;
  }

  @keyframes hd {
    50% {
      transform: rotate(45deg) scale(1.5);
    }
  }

  .heart::before {
    content: "";
    width: 200px;
    height: 200px;
    border-radius: 50%;
    position: absolute;
    background-color: red;
    transform: translateX(-100px);
  }

  .heart::after {
    content: "";
    width: 200px;
    height: 200px;
    border-radius: 50%;
    position: absolute;
    background-color: red;
    transform: translateY(-100px);
  }
</style>

<body>
  <div class="heart"></div>
</body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蛋蛋的老公

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值