关键帧(@keyframes)

@keyframes 规则通过在动画序列中定义关键帧(或waypoints)的样式来控制CSS动画序列中的中间步骤。这比转换更能控制动画序列的中间步骤。

1、语法

1.1、取值

  • 帧列表的名称。 名称必须符合 CSS 语法中对标识符的定义。
  • from 等效于 0%
  • to 等效于 100%
  • 动画序列中,触发关键帧的时间点,使用百分值来表示。

1.2、语法格式

@keyframes <keyframes-name> {
  <keyframe-block-list>
}
where 
<keyframes-name> = <custom-ident> | <string>
<keyframe-block-list> = <keyframe-block>+

where 
<keyframe-block> = <keyframe-selector># {
  <declaration-list>
}

where 
<keyframe-selector> = from | to | <percentage>

2、案例分析

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>css关键帧动画</title>

        <style>

            /* 让元素做关键帧动画: */
            /* 第一步,创建动画 */

            @keyframes move{
                /* 0%表示动画开始的关键帧 */
                0%{
                    transform: translate(0,0);
                    animation-timing-function: ease-out;
                }

                25%{
                    transform: translate(300px,0);
                    animation-timing-function: ease-in;
                }
                50%{
                    transform: translate(300px,300px);
                }
                75%{
                    transform: translate(0px,300px);
                }

                /* 动画结束时的关键帧 */
                0%{
                    transform: translate(0,0);
                }
            }

            #box{
                width: 100px;
                height: 100px;
                background-color: red;

                /* 第二步,把动画添加到元素上 */

                /* animation-name:动画名字,设置本元素要执行哪个动画 */
                animation-name: move;

                /* 设置单次动画的执行时间 */
                animation-duration: 2s;
                /* 动画状态是否保留,设置为both可以保留动画结束时元素的样式 */
                animation-fill-mode: both;
                /* 设置动画执行速率,默认为ease-in-out,匀速使用linear */
                animation-timing-function: linear;

                /* 动画延时时间 */
                animation-delay: 0s;

                /* 设置动画方向 */
                /* animation-direction: reverse; */

                /* 动画执行次数,默认为1,设置为infinite表示无限次 */
                animation-iteration-count: infinite;
            }
            #d2{
                width: 100px;
                height: 100px;
                background-color: blue;
                margin: 50px auto;
                text-align: center;
                line-height: 100px;
            }

            @keyframes spin{
                0%{
                    transform: rotate(0deg);
                }

                100%{
                    transform: rotate(360deg);
                }
            }
            .spin{
                animation-name: spin;
                animation-duration: 2s;
                animation-timing-function: linear;
                animation-iteration-count: infinite;

            }
            #d2:hover{
                /* animation-play-state,设置动画执行状态,默认为running,设置为paused可以暂停动画 */
                animation-play-state: paused;
                cursor: pointer;
            }
            #d2:active{
                animation-play-state: running;
                animation-direction: reverse;
            }
        </style>

    </head>
    <body>
    <div id="box"></div>
    <div id="d2" class="spin">旋转动画</div>
    </body>
</html>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8BOXCcJ2-1584693547477)(D:\DAYNOTE\keyframe.PNG)]

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值