CSS3-动画

动画(animation)

  1. 什么是动画

    • 动画是 CSS3 中最具颠覆性的特征之一,可通过设置多个节点来精确的控制一个或者一组动画,从而实现复杂的动画效果
  2. 动画的基本使用

    • 先定义动画
    • 在调用定义好的动画
  3. 语法格式(定义动画)

@keyframes 动画名称 {
    0% {
        width: 100px;
    }
    100% {
        width: 200px
    }
}
  1. 语法格式(使用动画)
div {
	/* 调用动画 */
    animation-name: 动画名称;
 	/* 持续时间 */
 	animation-duration: 持续时间;
}
  1. 动画序列
  • 0% 是动画的开始,100 % 是动画的完成,这样的规则就是动画序列
  • 在 @keyframs 中规定某项 CSS 样式,就由创建当前样式逐渐改为新样式的动画效果
  • 动画是使元素从一个样式逐渐变化为另一个样式的效果,可以改变任意多的样式任意多的次数
  • 用百分比来规定变化发生的时间,或用 fromto,等同于 0% 和 100%
  1. 代码演示
<style>
    div {
      width: 100px;
      height: 100px;
      background-color: aquamarine;
      animation-name: move;
      animation-duration: 0.5s;
    }

    @keyframes move{
      0% {
        transform: translate(0px)
      }
      100% {
        transform: translate(500px, 0)
      }
    }
  </style>

案例:动画序列

<!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 move {
            0% {
                transform: translate(0, 0);
            }
            25% {
                transform: translate(1000px, 0px);
            }
            50% {
                transform: translate(1000px, 500px);
            }
            75% {
                transform: translate(0px, 500px);
            }
            100% {
                transform: translate(0, 0);
            }
        }
        
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
            /* animation: name duration timing-function delay iteration-count direction fill-mode; */
            animation: move 5s linear infinite;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>

动画常见属性

属性描述
@keyframes规定动画
animation所有动画属性的简写属性,除了animation-play-state属性
animation-name@keyframes动画的名称 (必填)
animation-duration动画完成一个周期的时间,默认是0(必填)
animation-timing-function动画的运动曲线,默认是ease
animation-delay规定动画从什么时候开始,默认是0
animation-iteration-count动画的播放次数,默认是1,还有infinite
animation-direction规定动画下一周期逆向播放,默认是“normal”,“alternate”逆播放
animation-play-state规定动画是否是在运行或暂停。默认是 ;running ,还有paused
animation-fill-mode动画结束后,保持forwords回到起始backwards

代码演示

div {
  width: 100px;
  height: 100px;
  background-color: aquamarine;
  /* 动画名称 */
  animation-name: move;
  /* 动画花费时长 */
  animation-duration: 2s;
  /* 动画速度曲线 */
  animation-timing-function: ease-in-out;
  /* 动画等待多长时间执行 */
  animation-delay: 2s;
  /* 规定动画播放次数 infinite: 无限循环 */
  animation-iteration-count: infinite;
  /* 是否逆行播放 */
  animation-direction: alternate;
  /* 动画结束之后的状态 */
  animation-fill-mode: forwards;
}

div:hover {
  /* 规定动画是否暂停或者播放 */
  animation-play-state: paused;
}

动画简写方式

  1. 动画简写方式

    /* animation: 动画名称 持续时间 运动曲线 何时开始 播放次数 是否反方向 起始与结束状态 */
    animation: name duration timing-function delay iteration-count direction fill-mode
    
  2. 知识要点

    • 简写属性里面不包含 animation-paly-state
    • 暂停动画 animation-paly-state: paused; 经常和鼠标经过等其他配合使用
    • 要想动画走回来,而不是直接调回来:animation-direction: alternate
    • 盒子动画结束后,停在结束位置:animation-fill-mode: forwards
  3. 代码演示

animation: move 2s linear 1s infinite alternate forwards;

速度曲线细节

  1. 速度曲线细节
  • animation-timing-function: 规定动画的速度曲线,默认是ease
描述
linear从头到尾速度相同,匀速
ease默认,低速开始,加速,末尾减速
ease-in动画以低速开始
ease-out动画以低速结束
ease-in-out动画以低速开始和结束
steps()指定了时间函数中的间隔数量(步长)
  1. 代码演示
div {
  width: 0px;
  height: 50px;
  line-height: 50px;
  white-space: nowrap;
  overflow: hidden;
  background-color: aquamarine;
  animation: move 4s steps(24) forwards;
}

@keyframes move {
  0% {
    width: 0px;
  }

  100% {
    width: 480px;
  }
}

案例 :奔跑的白熊

<!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>
        body {
            background: #ccc;
        }
        
        div {
            position: absolute;
            width: 200px;
            height: 100px;
            background: url(img/bear.png) no-repeat;
            /* 可以写多个动画,用,分隔 */
            animation: bear .4s steps(8) infinite, move 3s forwards;
        }
        
        .div1 {
            margin-top: 240px;
        }
        
        .bg {
            width: 99%;
            height: 450px;
            background: url(img/bg1.png) no-repeat;
            /* animation: name duration timing-function delay iteration-count direction fill-mode; */
            animation: moveBg 8s linear infinite forwards;
        }
        
        @keyframes bear {
            0% {
                background-position: 0 0;
            }
            100% {
                background-position: -1600px 0;
            }
        }
        
        @keyframes move {
            0% {
                top: 0;
                left: 0;
            }
            100% {
                top: 0;
                left: 50%;
                transform: translateX(-50%);
            }
        }
        
        @keyframes moveBg {
            0% {
                background-position: 0 0;
            }
            100% {
                background-position: -2000px 0;
            }
        }
    </style>
</head>

<body>
    <div class="bg"></div>
    <div class="div1"></div>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值