【问题标题】:svg loader animation loopsvg 加载器动画循环
【发布时间】:2021-01-07 14:47:25
【问题描述】:

我需要一些帮助来创建我的 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>

【问题讨论】:

  • 你也只需要 CSS 动画或 smil SVG 吗?
  • @Alexandr_TT 仅动画正常工作
  • 我理解正确,只需要 CSS 动画?
  • @Alexandr_TT 确切地说,我的动画包含在 sn-p 中,但正如我所说,它不能正常工作。完成后它应该向后播放:)

标签: css svg css-animations


【解决方案1】:

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

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

  2. 您有一条路径,并为 stroke-dashoffset 设置动画。问题是路径的各个部分具有不同的长度,因此无法知道何时开始对圆圈进行动画处理。我使用 5 条路径而不是仅一条路径,并且在路径动画结束时开始循环动画。

  3. 在这种情况下,您不能使用动画方向:交替;相反,我使用了 2 个连接动画。我还需要一些 javascript 来知道第二个动画何时结束,以便我可以删除类 svg 并稍后将其添加回来。

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

作为观察:正如@Alexandr_TT 在它的 cmets 中提到的那样,在这种情况下,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>

【讨论】:

  • 很好的答案!使用变量的有趣方式。在实践中记住和使用它们是有益的。谢谢恩萨内塔!处处感受到大师之手
  • @Alexandr_TT 这太棒了。我必须让它快一点,并尝试解决为什么当最后一个圆圈变成橙色时它会闪烁。
  • @Marek 如果您喜欢 enxaneta 的回答,那么最好为他的回答点赞
  • @enxaneta 你能告诉我为什么当线画到最后一个圆圈时,最后一个圆圈只会闪烁吗?想不通。
  • 因为它立即返回。到达那里后,圆圈将颜色从蓝色变为橙色,接下来另一个动画开始,圆圈变回蓝色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2023-01-15
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多