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>

多元素示例

相关推荐
GISer_Jing3 小时前
CSS学习路线
前端·css·学习
职场人参15 小时前
将多个pdf合并成一个文件?这几种合并方法很好用!
linux·前端·css
JohnsonXin18 小时前
【兼容性记录】video标签在 IOS 和 安卓中的问题
android·前端·css·ios·h5·兼容性
qq_424317181 天前
html+css+js网页设计 旅游 大理旅游7个页面
css·html·旅游
安冬的码畜日常2 天前
【CSS in Depth 2 精译_029】5.2 Grid 网格布局中的网格结构剖析(上)
前端·css·css3·html5·grid·css布局·grid布局
安冬的码畜日常2 天前
【CSS in Depth 2 精译_030】5.2 Grid 网格布局中的网格结构剖析(下)
前端·css·css3·html5·flexbox·网格布局·css布局
qq_424317182 天前
html+css网页设计 旅游网站首页1个页面
css·html·旅游
Cat God 0072 天前
胡学乱想----前端知识点(css色彩)
前端·css
雪芽蓝域zzs2 天前
Html css水平居中+垂直居中+水平垂直居中的方法总结
前端·css·html
懒羊羊大王呀2 天前
CSS——网格布局(display: grid)之上篇
前端·css