改变SVG动画的方向

the*_*ook 8 css svg

我看到了这个SVG动画,我想知道如何改变线被删除的方向; 当前线从它绘制的最后一点缩回,但是我想要反过来; 从第一次开始绘制的点开始擦除自己的行(这样看起来更像是一个加载动画).

我看到.path上的动画属性值为无穷大,但我不确定如何指定方向.

HTML是

<div class="bg">  
<svg xmlns="http://www.w3.org/2000/svg" width="670" height="236" viewBox="0 0 670 236">

  <path class="path" stroke="#4CADC1" stroke-width="4" stroke-linejoin="round" stroke-linecap="round" stroke-miterlimit="10" stroke-dasharray="300" stroke-dashoffset="300" fill="none" d="M343.6 75.9v20.3l23.1 21.8-23.1 21.8v20.3l44.6-42.1zM326.4 139.8l-23.1-21.8 23.1-21.8v-20.3l-44.6 42.1 44.6 42.1z"/>

  <path class="path" stroke="#4CADC1" stroke-width="4" stroke-linejoin="round" stroke-linecap="round" stroke-miterlimit="10" stroke-dasharray="500" stroke-dashoffset="500" fill="none" d="M335 38.9c-43.7 0-79.1 35.4-79.1 79.1s35.4 79.1 79.1 79.1 79.1-35.4 79.1-79.1-35.4-79.1-79.1-79.1zM335 182.9c-35.8 0-64.9-29.1-64.9-64.9s29.1-64.9 64.9-64.9 64.9 29.1 64.9 64.9-29.1 64.9-64.9 64.9z"/>

  </svg>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS就是

body {
  background-color: #fff;
}

.bg  {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.path {
  animation: draw 3.5s infinite;
}

 @keyframes draw {
  50% {
    stroke-dashoffset: 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

Per*_*ijn 6

我喜欢您将其制作为加载动画的想法:

CODEPEN

现在我做了什么:

更改了动画的开始停止点

@keyframes draw {
  100% {
    stroke-dashoffset: -500;
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么是-500?
因为这是破折号数组的值。
这在<svg>中定义:dasharray="500"

在最里面的路径中更改了该值。只有300

我添加了线性动画

animation: draw 5s infinite linear;
Run Code Online (Sandbox Code Playgroud)

默认设置为easy。我发现该动画与线性动画具有更好的一致性。

注意

dashoffset=500 <-使动画开始时没有破折号/描边

  • 负值确实会反转方向,但它在 Safari 中似乎无法正常工作(在 Chrome 和 FF 中效果很好)。 (2认同)