css动画

什么是动画

动画可以使元素在规定的时刻做出样式的改变和位置的改变等等

@keyframes

keyframes是关键帧的意思,我们使用@keyframes 时,可以用两种方式

  1. from…to…
    例子:
    圆球将会发生颜色的转变
<!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>Document</title>
</head>
<style>
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 10s 1 ;
    }
    /* 关键帧 */
    @keyframes myannimal{
       form{
           background-color: blue;
       }
       to{
           background-color: cadetblue;
       }
    }
</style>
<body>
    <div>

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

效果如下
在这里插入图片描述

  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>Document</title>
</head>
<style>
    body{
        border: red solid 10px;
        width: 800px;
        height: 700px;
    }
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 10s 1 ;
    }
    /* 关键帧 */
    @keyframes myannimal{
        0% {
            transform: translateX(0) translateY(0);
        }
        25%{
            transform: translateX(600px) translateY(0);
        }
        50%{
            transform: translateX(600px) translateY(500px);
        }
        75%{
            transform: translateX(0) translateY(500px);
        }
        100%{
            transform: translateX(0) translatey(0);
        }
    }
</style>
<body>
    <div>

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

效果如下:
在这里插入图片描述

animation的相关属性

关键帧包含元素在特定时间所拥有的样式。 当我们选用该元素时,样式会默认出现,我们只需依次填好,再把不用的删去就可以了。

animation: name duration timing-function delay iteration-count direction fill-mode;
属性描述
animation-name动画名称
animation-duration动画的持续时间
animation-timing-function动画的过渡类型
animation-delay动画延迟的时间
animation-iteration-count动画的循环次数
animation-direction动画在循环中是否反向运动
animation-fill-mode动画时间之外的状态
animation-play-state对象动画的状态
  • 持续时间 animation-duration
    例子:
<!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>Document</title>
</head>
<style>
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 10s ;
    }
    /* 关键帧 */
    @keyframes myannimal{
       form{
           background-color: blue;
       }
       to{
           background-color: cadetblue;
       }
    }
</style>
<body>
    <div>

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

该动画将会持续10s。
效果图如下:
在这里插入图片描述

  • 过渡类型 animation-timing-function
属性描述
linear线性过渡
ease平滑过渡
ease-in由慢到快
ease-out由快到慢
ease-in-out开始和结束较慢
cubic-bezier(n,n,n,n)自己定义
  • 延迟时间animation-delay
    例子:
<style>
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 10s ease-in 2s;
    }

该变色在开始前有 2 秒的延迟
效果图如下:
在这里插入图片描述

  • 循环次数animation-iteration-count
  • infinite表示无限循环
    例子:
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 3s ease-in 3;
    }

该动画将会进行三次

在这里插入图片描述

  • 是否反向运动animation-direction
属性描述
normal正常方向
reverse反方向运行
alternate动画先正常运行再反方向运行,并持续交替运行
alternate-reverse动画先反运行再正方向运行,并持续交替运行

例子:

    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 1s reverse;
    }

该效果将会反方向运行
在这里插入图片描述

  • 动画时间之外的状态animation-fill-mode
属性样式
none默认值。不设置对象动画之外的状态
forwards设置对象状态为动画结束时的状态
backwards设置对象状态为动画开始时的状态
both设置对象状态为动画结束或开始的状态
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 1s both;
    }

效果图为动画运行结束后的状态
1.none(开始状态)
在这里插入图片描述
2.forwards(结束状态)

在这里插入图片描述
3.backwards(开始状态)
在这里插入图片描述
4.both(结束状态)
在这里插入图片描述

  • 对象动画的状态 animation-play-state
属性描述
running运动
paused暂停
    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 1s running;
    }

动画正常运行
2.

    div{
        height: 200px;
        width: 200px;
        background-color: aqua;
        border-radius: 50%;
        animation: myannimal 1s paused;
    }

动画暂停,没有运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值