ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

css 动画

2020-06-01 16:08:39  阅读:218  来源: 互联网

标签:动画 关键帧 width animation background css


css动画
首先要明白动画是一帧一帧的,由多个帧拼起来的动画

@keyframes

此为动画样式中的关键帧,用关键帧来控制css动画中的关键样式。相比较过渡更加的容易空值中间的部分
其指示了一个过程到另一个过程的过程
关键帧还具有名字,在应用的时候通过名字将其绑定。

如果关键帧重复定义,则根据最后一次定义为准

关键帧中的important会被略过

举个栗子

定义一个关键帧

@keyframes myFrames {
    form {
        background:#a7e5c6;
        width:100px;
    }

    to {
        width:90%;
        background:#c6c2a3;
    }
}

样式如下
此处输入图片的描述

这样就完成一次动画操作

分开定义

也可以进行分开定义
按照百分号进行定义,结果如下
关键帧如下

@keyframes myFrames {
    0% {
        width:200px;
        background:#827e64;
    }

    20% {
        width:400px;
        background:#86bece;
    }

    50% {
        height:600px;
        background:#af92aa;
    }

    90% {
        width:300px;
        height:400px;
        background:#698771;
    }
}

效果如下
此处输入图片的描述

animation

animation 同样是一个简写属性,相比较js写动画来说,css动画已经灰常简单了。

大概看了一点纯js动画,js动画核心在于对css样式的更改,外加一个重复时间对css不断的累加得到动画效果

下面依次说明

animation-name

和关键帧进行绑定
必须和关键帧的名字相同(废话)

animation-duration

指定一个动画的周期

负值的动画无效

举一个栗子

div {
    width:300px;
    height:400px;
    background:#698771;
    margin:auto;
    animation-name: myFrames;
    animation-duration:.9s;
}

/*关键帧*/
@keyframes myFrames {
    0% {
        width:200px;
        background:#827e64;
    }

    20% {
        width:400px;
        background:#86bece;
    }

    50% {
        height:600px;
        background:#af92aa;
    }

    90% {
        width:300px;
        height:400px;
        background:#698771;
    }
}

动画效果如下
此处输入图片的描述

animation-timing-function

定义一个动画的过程,类似于过渡的函数
同样的,有贝塞尔曲线等等
不在阐述

DevTools

谷歌浏览器的调试工具具有该方法,可以直接使用调试工具绘制贝塞尔曲线
此处输入图片的描述

animation-delay

定义动画的延迟
此处输入图片的描述
css如下

* {
    margin:0;
    padding:0;
}
body {
    position:relative;
}
div {
    width:400px;
    height:400px;
    position: absolute;
    left:0;
    top:0;
    bottom:0;
    margin:auto;
    background:#698771;
    border-radius:1000px;
    animation-name: myFrames;
    animation-duration:5s;
    animation-timing-function:cubic-bezier(0.785, 0.135, 0.15, 0.86);
    animation-delay:.9s;
}
div div {
    width:40px;
    height:40px;
    position:absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    margin:auto;
    background:#e8e3da;
    animation-name:myCenter;
}

/*关键帧*/
@keyframes myFrames {
    from {
        left:0;
    }

    to {
        left:70%;
    }
}

@keyframes myCenter {
    from {
        left:0;
    }

    to {
        left:0;
    }
}

html如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./index.css" type="text/css">
    <title>css动画</title>
</head>
<body>
    <div>
        <div></div>
    </div>
</body>
</html>
<script src="./index.js"></script>

动画延迟了0.9秒

animation-iteration-count

定义动画的迭代次数infinite 为永远重复
数值为number

举栗子

animation-iteration-count:3;

动画重复播放3次。

animation-iteration-count:infinite;

动画永远重复播放

animation-direction

定义是否向前,向后,是否交替来回

如果想要重复的多次播放,必须有animation-iteration-count的值为infinity否则不会出现重复播放

normal

为一个每次重复重新的位置开始播放(每次都将重置为新状态,开始执行)
此处输入图片的描述

reverse

倒序播放
此处输入图片的描述

alternate

奇数正向播放
偶数倒序播放
即来回
此处输入图片的描述

alternate-reverse

奇数倒序播放
偶数正向播放
即倒来回
此处输入图片的描述

ps 动画具有继承的属性

animation-fill-mode

forwards

将会保留最后一个关键帧,让其停留。
此处输入图片的描述
css

    /*animation-iteration-count:infinite;*/
    animation-direction:alternate;
    animation-fill-mode:forwards;

backwards

将会应用第一个动画值
此处输入图片的描述
和none的区别在于none使用默认的css样式,backwards将会使用动画的第一帧

    /*animation-iteration-count:infinite;*/
    animation-direction:alternate;
    animation-fill-mode:backwards;
ps 加上注释的原因是因为如果不加将会重复播放。

both

将会遵守倒序还是正序的停留

正序

    /*animation-iteration-count:infinite;*/
    animation-direction:normal;
    animation-fill-mode:both;

此处输入图片的描述

倒序

此处输入图片的描述

    /*animation-iteration-count:infinite;*/
    animation-direction:reverse;
    animation-fill-mode:both;

总写

按照上方顺序即可
css 如下

* {
    margin:0;
    padding:0;
}
body {
    position:relative;
}
div {
    width:400px;
    height:400px;
    position: absolute;
    left:0;
    top:0;
    bottom:0;
    margin:auto;
    background:#698771;
    border-radius:1000px;
    animation:myFrames 5s cubic-bezier(0.785, 0.135, 0.15, 0.86) .5s infinite alternate both;
}
div div {
    width:40px;
    height:40px;
    position:absolute;
    left:0;
    right:0;
    top:0;
    bottom:0;
    margin:auto;
    background:#e8e3da;
    animation-name:myCenter;
}

/*关键帧*/
@keyframes myFrames {
    from {
        left:0;
    }

    to {
        left:70%;
    }
}

@keyframes myCenter {
    from {
        left:0;
    }

    to {
        left:0;
    }
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./index.css" type="text/css">
    <title>css动画</title>
</head>
<body>
    <div>
        <div></div>
    </div>
</body>
</html>
<script src="./index.js"></script>

演示效果 https://melovemingming.gitee....

标签:动画,关键帧,width,animation,background,css
来源: https://www.cnblogs.com/baimeishaoxia/p/13025662.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有