css3动画之@keyframes关键帧

 时间:2026-02-15 13:07:31

1、@keyframes的基本语法:

@keyframes animationname {keyframes-selector {css-styles;}}

其中:

animationname:动画名称

keyframes-selector:动画时长的百分比

css-styles: CSS 样式属性

例子:

css部分:

.div1{

        position: relative;

        width:100px;

        height:100px;

        background: darkslategray       

        animation:myfirst 5s infinite;

        -webkit-animation:myfirst 5s infinite;

        -moz-animation:myfirst 5s infinite;

        -o-animation:myfirst 5s infinite;

    }

    @keyframes myfirst{

        0%{left:0px;}

        100%{left:300px;}

    }

html部分:

<div class="div1">keyframes动画示例</div>

效果如图:

css3动画之@keyframes关键帧

2、在一个动画中改变多个 CSS 样式

例子:

css部分:

.div1{

        position: relative;

        width:100px;

        height:100px;

        color:#fff;

        background: darkslategray;

        animation:myfirst 5s infinite;

        -webkit-animation:myfirst 5s infinite;

        -moz-animation:myfirst 5s infinite;

        -o-animation:myfirst 5s infinite;

    }

    @keyframes myfirst{

        0%{

            left:0px;

            top: 0px;

        }

        100%{

            left:300px;

            top:400px;

        }

    }

html部分:

<div class="div1">keyframes动画示例</div>

效果如图:

css3动画之@keyframes关键帧

3、带有多个 CSS 样式的多个 keyframe 选择器

例子:

css部分:

.div1{

        position: relative;

        width:200px;

        height:200px;

        color:#fff;

        background: darkslategray;

        animation:myfirst 5s infinite;

        -webkit-animation:myfirst 5s infinite;

        -moz-animation:myfirst 5s infinite;

        -o-animation:myfirst 5s infinite;

    }

    @keyframes myfirst{

        0%{

            left:0px;

            top: 0px;

            background: darkslategray;

        }

        25%{

            left:100px;

            top:150px;

            background: rosybrown;

        }

        50%{

            left: 150px;

            top:300px;

            background: red;

        }

        75%{

            left:300px;

            top:400px;

            background: goldenrod;

        }

        100%{

            left:0px;

            top:0px;

            background:darkorchid;

        }

    }

html部分:

<div class="div1">keyframes动画示例多个 CSS 样式的多个 keyframe 选择器</div>

效果如图:

css3动画之@keyframes关键帧

css3动画之@keyframes关键帧

css3动画之@keyframes关键帧

css3动画之@keyframes关键帧

4、keyframe兼容性代码具体如下:

@keyframes myfirst

        {

            from {top:0px;}

            to {top:200px;}

        }

        @-moz-keyframes myfirst/* Firefox */

        {

            from {top:0px;}

            to {top:200px;}

        }

        @-webkit-keyframes myfirst /* Safari 和 Chrome */

        {

            from {top:0px;}

            to {top:200px;}

        }

        @-o-keyframes myfirst /* Opera */

        {

            from {top:0px;}

            to {top:200px;}

        }

css3动画之@keyframes关键帧

  • 怎样快速煮小米粥
  • IKEA宜家家居如何查看优惠券
  • 安利泰国有哪些好用的保健品
  • 怎样判断尖疣湿锐早期
  • 如何和打呼噜的人一起睡觉,和打鼾的一起睡觉
  • 热门搜索
    电子档是什么 cdc是什么意思 什么是贸易公司 中国运动员 空调制冷效果不好是什么原因 凤凰涅磐是什么意思 copyright是什么意思 300多分能上什么大学 温江有什么好玩的地方 盗墓笔记结局是什么