• css3动画基础详解(@keyframes和animation)


    动画是使元素从一种样式逐渐变化为另外一种效果,CSS3动画的生成,主要依赖@keyframes定义动画,animation执行动画。

    @keyframes

    通过 @keyframes 规则创建动画。

    @keyframes keyframes-name {keyframes-selector {css-styles;}}
    keyframes-name 帧列表的名称。 名称必须符合 CSS 语法中对标识符的定义。
    keyframes-selector 动画时长的百分比。合法值:
    0-100%
    from 等效于 0%
    to 等效于 100%
    css-styles 需要改变的css样式,支持多属性

    animation

    animation 是一个简写的属性,用于设置6个动画属性:

    • animation-name:这个就是使用@keyframes定义的动画名称;
    • animation-duration:动画执行的时间,以秒为单位
    • animation-delay:规定动画开始之前的延迟
    • animation-iteration-count:规定动画应该播放的次数,n(次数) | infinite(无限次)
    • animation-direction:规定是否应该轮流反向播放动画
    • animation-timing-function:规定动画的速度曲线
    1. @keyframes changeSize {
    2. 0% {
    3. transform: scale(0.8);
    4. }
    5. 50% {
    6. transform: scale(1.2);
    7. }
    8. 100% {
    9. transform: scale(0.8);
    10. }
    11. }
    12. .demo {
    13. animation-name: changeSize;
    14. animation-duration: 2s;
    15. animation-iteration-count:infinite;
    16. }
    animation-timing-function

    这里说下复杂属性,第一个是animation-timing-function规定动画速度的曲线

    说明
    ease默认,低速开始,然后加快,结束前变慢
    linear从头到尾速度相同
    ease-in以低速度开始,先慢后快
    ease-out以低速结束,先快后慢
    ease-in-out以低速开始和结束
    cubic-bezier(x1,y1,x2,y2)在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值。

    cubic-bezier:三次赛贝尔曲线函数,前面的几个预设函数都可以通过它来实现。 通过控制曲线上的四个点(起始点(0,0)、终止点(1,1)以及两个相互分离的中间点)来绘制一条曲线并以曲线的状态来反映动画过程中速度的变化。可以访问 cubic-bezier.com 来设置对应的值。

    ease 的效果等同于 cubic-bezier(.25,.01,.25,1)

    linear 的效果等同于 cubic-bezier(0,0,1,1)

    ease-in 的效果等同于 cubic-bezier(.42,0,1,1)

    ease-out 的效果等同于 cubic-bezier(0,0,.58,1)

    ease-in-out 的效果等同于 cubic-bezier(.42,0,.58,1)

    1. @keyframes dropdown {
    2. 0% {
    3. top: 0px;
    4. }
    5. 100% {
    6. top: 420px;
    7. }
    8. }
    9. ul li{
    10. &:first-child{
    11. animation: dropdown 6s ease infinite;
    12. }
    13. &:nth-child(2){
    14. animation: dropdown 6s linear infinite;
    15. }
    16. &:nth-child(3){
    17. animation: dropdown 6s ease-in infinite;
    18. }
    19. &:nth-child(4){
    20. animation: dropdown 6s ease-out infinite;
    21. }
    22. &:nth-child(5){
    23. animation: dropdown 6s ease-in-out infinite;
    24. }
    25. &:last-child{
    26. animation: dropdown 6s cubic-bezier(.08,.6,.67,1.03) infinite;
    27. }
    28. }
    animation-direction

    animation-direction 定义是否应该轮流反向播放动画,如果动画次数设置为一次,则无效。

    说明
    normal默认,正常播放
    reverse动画反向播放
    alternate交替播放, 动画会在奇数次数(1、3、5 等等)正常播放,而在偶数次数(2、4、6 等等)反向播放。
    alternate-reverse交替播放, 动画会在奇数次数(1、3、5 等等)反向播放,而在偶数次数(2、4、6 等等)正常播放。

    我们可以把上面的案例每个都加上alternate的参数,再看下效果,是不是更赞了?

    1. ul li{
    2. &:first-child{
    3. animation: dropdown 6s ease infinite alternate;
    4. }
    5. &:nth-child(2){
    6. animation: dropdown 6s linear infinite alternate;
    7. }
    8. &:nth-child(3){
    9. animation: dropdown 6s ease-in infinite alternate;
    10. }
    11. &:nth-child(4){
    12. animation: dropdown 6s ease-out infinite alternate;
    13. }
    14. &:nth-child(5){
    15. animation: dropdown 6s ease-in-out infinite alternate;
    16. }
    17. &:last-child{
    18. animation: dropdown 6s cubic-bezier(.08,.6,.67,1.03) infinite alternate;
    19. }
    20. }

     旋转

    1. img {
    2. width: 315px;
    3. height: 315px;
    4. animation: circle 10s infinite linear;
    5. @keyframes circle {
    6. from {
    7. transform: rotate(0);
    8. }
    9. to {
    10. transform: rotate(360deg);
    11. }
    12. }
    13. }
    14. //
    15. @keyframes circleAnimate {
    16. 0%{
    17. opacity: .3;
    18. }
    19. 100%{
    20. opacity: 1;
    21. }
    22. }
    23. .l{
    24. left:0;
    25. &::before{
    26. position: absolute;
    27. width:100%;
    28. height:100%;
    29. content: "";
    30. background: url(./img/l1.min.png);
    31. animation: circleAnimate 1s ease-in-out 0s infinite;
    32. }
    33. &::after{
    34. position: absolute;
    35. width:100%;
    36. height:100%;
    37. content: "";
    38. background: url(./img/l1-2.min.png);
    39. animation: circleAnimate 1s ease-in-out 0.5s infinite;
    40. }
    41. }

     

  • 相关阅读:
    SpringBoot项目--电脑商城【用户注册】
    【Spring源码分析】Bean的元数据和一些Spring的工具
    【手写数字识别】CNN卷积神经网络入门案例
    算法训练第六十五天|螺旋遍历二维数组
    200PPI转以太网与易控modbusTCP客户端通信配置
    第二章 经典同步练习作业
    【图神经网络论文整理】(二)—— HOW ATTENTIVE ARE GRAPH ATTENTION NETWORKS?:GATv2
    05- 基于UDS协议的故障代码状态字节及检测机制
    Nginx 前端 安装,打包,发布项目到服务 流程
    SSL证书一次性购买多年期,有什么好处?
  • 原文地址:https://blog.csdn.net/weixin_37935725/article/details/132976013