SVG 路径动画的实现原理

SVG 路径动画的实现原理

svg 路径动画的实现,主要依赖于两个 svg 属性(同样适用CSS),它们分别是:

  • stroke-dasharray : 定义用于绘制形状轮廓的虚线和间隙
  • stroke-dashoffset: stroke-dasharray 的偏移量
html 复制代码
<svg viewBox="0 0 30 12" xmlns="http://www.w3.org/2000/svg">
  <line x1="0" y1="1" x2="30" y2="1"></line>
</svg>
<style>
  svg {
    width: 400px;
    border: 1px solid #333;
  }
  line {
    stroke: white;
    stroke-dasharray: 10;
    animation: move 2s infinite;
  }
  @keyframes move {
    0%,
    100% {
      stroke-dashoffset: 0;
    }
    50% {
      stroke-dashoffset: 10;
    }
  }
</style>

如果 stroke-dashoffsetstroke-dashoffset 以及 路径的长度相同, 就可以实现元素从消失到完整出现的过渡效果。 如下:

css 复制代码
  @keyframes move {
    0%,
    100% {
      stroke-dashoffset: 0;
    }
    50% {
      stroke-dashoffset: 30;
    }
  }

但是有两个问题我们需要解决:

  1. 我们需要知道路径的长度
  2. 我们需要设定动画为变量

第一个问题,我们可以通过svg 元素对象的API 实现:getTotalLength()

getTotalLength()

js 复制代码
document.querySelector('line').getTotalLength();// 30

第二个问题,我们可以通过 EL.style.setProperty 和 CSS 变量引用var 来解决:

style.setProperty

示例:

js 复制代码
// js
const p = document.querySelector('line')
p.style.setProperty('--l', p.getTotalLength())
css 复制代码
/* css */
  line {
    stroke: white;
    stroke-dasharray: var(--l);
    animation: move 2s infinite;
  }
  @keyframes move {
    0%,
    100% {
      stroke-dashoffset: 0;
    }
    50% {
      stroke-dashoffset: var(--l);
    }
  }

多路径示例

html 复制代码
  <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24">
    <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
      color="currentColor">
      <path class="p" d="M22 12c0 5.523-4.477 10-10 10S2 17.523 2 12S6.477 2 12 2s10 4.477 10 10" />
      <path class="p" d="M2 12c2.28 2.429 5.91 4 10 4s7.72-1.571 10-4M12 2v20" />
      <path class="p"
        d="M5.156 5c-.382 1.5-.116 4 1.41 6c1.605 2.103 2.616 5-1.197 8M18.844 5c.382 1.5.116 4-1.41 6c-1.605 2.103-2.616 5 1.197 8" />
    </g>
  </svg>
html 复制代码
<style>
  svg {
    width: 400px;
    border: 1px solid #333;
    color: white;
  }
  .p {
    stroke: white;
    stroke-dasharray: var(--l);
    animation: move 2s infinite;
  }
  @keyframes move {
    0%,
    100% {
      stroke-dashoffset: 0;
    }
    50% {
      stroke-dashoffset: var(--l);
    }
  }
</style>
html 复制代码
<script>
  const paths = document.querySelectorAll('.p')
  paths.forEach(p => {
    p.style.setProperty('--l', p.getTotalLength())
  })
</script>

多元素示例

相关推荐
喵手5 分钟前
CSS3 渐变、阴影和遮罩的使用
前端·css·css3
十年砍柴---小火苗2 小时前
原生js操作元素类名(classList,classList.add...)
javascript·css·css3
站在风口的猪11088 小时前
《前端面试题:CSS对浏览器兼容性》
前端·css·html·css3·html5
站在风口的猪110812 小时前
《前端面试题:前端布局全面解析(圣杯布局、双飞翼布局等)》
前端·css·html·css3·html5
啪叽15 小时前
CSS函数即将到来——它们将改变你的编码方式
前端·css
前端小趴菜0517 小时前
CSS设置移动端页面底部安全距离
前端·css
吃杠碰小鸡17 小时前
css-塞贝尔曲线
前端·css
棉花糖超人18 小时前
【从0-1的HTML】第3篇:html引入css的3种方式
前端·css·html
爱编程的鱼20 小时前
如何使用 HTML、CSS 和 JavaScript 随机更改图片颜色
javascript·css·html
databook1 天前
Manim中三种函数图像类的比较
python·动效