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. 您有一条路径,并设置笔划dashoffset的动画。问题是,路径的各个部分具有不同的长度,因此无法知道何时开始制作圆的动画。我使用的不是一条路径,而是5条路径,并且在路径动画的末尾开始圆形动画。

  3. 在这种情况下,不能使用动画方向:交替;相反,我使用了两个串联的动画。我还需要一些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,244L216,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,71L478,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,339L762,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,50L948,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,238L1139,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>

最新更新