svg加载器动画循环

如何解决svg加载器动画循环

我需要一些帮助来创建我的SVG加载器。我下面有一个这样的动画。动画开始时,每个圆圈变为橙色。完成后,线开始向后画(可以),但每个圆圈也应变为蓝色。而且它也应该循环播放。你们能帮我吗?包含摘要。

svg {
 width: 100%;
            max-width: 500px;
}

path {
              stroke-dasharray: 2530;
              stroke-dashoffset: 2530;
              animation: draw 1.5s linear infinite alternate;
            }

.circle-big {
  fill: #6085A1;
}

.circle-small {
              fill: #fff;
            }

#circle-1-big {
                animation: changeColor;
                animation-duration: 100ms;
                animation-fill-mode: forwards;
                animation-delay: 0ms;
              }
              
#circle-2-big {
                animation: changeColor;
                animation-duration: 100ms;
                animation-fill-mode: forwards;
                animation-delay: 100ms;
              }              
#circle-3-big {
                animation: changeColor;
                animation-duration: 100ms;
                animation-fill-mode: forwards;
                animation-delay: 350ms;
              }
#circle-4-big {
                animation: changeColor;
                animation-duration: 100ms,100ms;
                animation-fill-mode: forwards;
                animation-delay: 650ms;
              }
#circle-5-big {
                animation: changeColor;
                animation-duration: 100ms,100ms;
                animation-fill-mode: forwards;
                animation-delay: 800ms;
              }
@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes changeColor {
  0%   {
    fill: #6085A1;
  }
  100% {
    fill: #EF7B00;
  }
}
<div class="loader">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1228 408">
      <g>
        <path stroke-linecap="round" stroke-width="11" stroke="black" fill="none" d="M51,244L216,71L478,339L762,50L948,238L1139,48"></path>
        <circle class="circle-big" id="circle-1-big" cx="51" cy="244" r="39"></circle><circle class="circle-small" id="circle-1-small" cx="51" cy="244" r="14"></circle>
        <circle class="circle-big" id="circle-2-big" cx="216" cy="71" r="39"></circle><circle class="circle-small" id="circle-2-small" cx="216" cy="71" r="14"></circle>
        <circle class="circle-big" id="circle-3-big" cx="478" cy="339" r="39"></circle><circle class="circle-small" id="circle-3-small" cx="478" cy="339" r="14"></circle>
        <circle class="circle-big" id="circle-4-big" cx="762" cy="50" r="39"></circle><circle class="circle-small" id="circle-4-small" cx="762" cy="50" r="14"></circle>
        <circle class="circle-big" id="circle-5-big" cx="1139" cy="48" r="39"></circle><circle class="circle-small" id="circle-5-small" cx="1139" cy="48" r="14"></circle>
      </g>
    </svg>
</div>

解决方法

我对您的代码进行了一些更改:

  1. 不是使用2个圆,而是使用一个粗笔划的圆。在这种情况下,我是粗略地设置笔画而不是填充动画。

  2. 您有一条路径,并为笔划破折号设置了动画。问题在于路径的各个部分具有不同的长度,从而使得无法知道何时开始绘制圆圈的动画。我正在使用5条路径,而不是仅一条路径,而是在路径动画的末尾开始圆形动画。

  3. 在这种情况下,您不能使用动画方向:相反,我正在使用2个串联的动画。我还需要一些JavaScript才能知道第二个动画何时结束,以便可以删除svg类并将其添加回去。

为了计算延迟,我使用css变量,但是您可能需要为每个段使用不同的动画(对于那些不支持变量的浏览器)。或者,您可以使用javascript。另外,我正在使用变量来存储每个路径的长度。

作为观察:正如@Alexandr_TT在其注释中提到的,在这种情况下,SMIL动画可能会更好。还有一个用于SMIL动画的js polyfill。

let svg = document.querySelector("svg");

let i = 0;

first.addEventListener(
  "animationend",() => {
    i++;
    if (i % 2 == 0) {
      svg.setAttribute("class","");
    }
  },false
);

window.setInterval( function(){
 svg.setAttribute("class","svg");
},15000/60);
svg {
  width: 100%;
  max-width: 500px;
}

path {
  stroke-dasharray: var(--sd);
  stroke-dashoffset: var(--sd);
  stroke:black;
}
.svg path {
  animation: draw 1s forwards calc(1.5s * var(--n)),draw1 1s forwards calc(1.5s * (9 - var(--n)))}

circle {
  stroke: #6085a1;
  stroke-width:30px;
  fill:white;
}
.svg circle {
  animation: a .5s  forwards calc(1.5s * var(--n) - .5s),b .5s  forwards calc(1.5s * (10 - var(--n))  - .5s);
 }


@keyframes draw {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes draw1 {
  to {
    stroke-dashoffset: var(--sd);
  }
}

@keyframes a {
  0%   {
    stroke: #6085A1;
  }
  100% {
    stroke: #EF7B00;
  }
}

@keyframes b {
  0% {
    stroke: #EF7B00;
  }
  100%   {
    stroke: #6085A1;
  }

}
<div class="loader">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1228 408" class="svg">
        <!--<path stroke-linecap="round" stroke-width="11" stroke="black" fill="none" d="M51,244L216,71L478,339L762,50L948,238L1139,48"></path>-->
      
        <g style="--sd:239;--n:0">
        <path id="a" stroke-width="11" d="M51,71"  />
        <circle cx="51" cy="244" r="30" id="first"></circle>
        </g>
        <g style="--sd:374.79;--n:1">
        <path id="b" stroke-width="11" d="M216,339" />
        <circle cx="216" cy="71" r="30"></circle>
        </g>
        <g style="--sd:405.19;--n:2">
        <path id="c" stroke-width="11" d="M478,50" />
        <circle cx="478" cy="339" r="30"></circle>
        </g>
        <g style="--sd:264.46;--n:3">
        <path id="d" stroke-width="11" d="M762,238" />
        <circle cx="762" cy="50" r="30"></circle>
        </g>
        <g style="--sd:269.4;--n:4">
        <path id="e" stroke-width="11" d="M948,48"  />
          <circle cx="948" cy="238" r="30"></circle>
        </g>
       <g style="--n:5">
         <circle cx="1139" cy="48" r="30"></circle>
       </g>
    </svg>
</div>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-