平时使用 svg,都是当成图片来使用,直接用 img 标签像引入图片一样引入 svg。 现在认识一下 SVG Sprite 技术。

https://www.cnblogs.com/jiduoduo/p/6657576.html (opens new window)

SVG Sprite 技术介绍

# 一、Sprite 技术

这里所说的 Sprite 技术,没错,类似于 CSS 中的 Sprite 技术。图标图形整合在一起,实际呈现的时候准确显示特定图标。

# 二、SVG Sprite 与 symbol 元素

目前 SVG Sprite 最佳实践是使用 symbol 元素。symbol 元素是什么呢?单纯翻译的话,是“符号”的意思。然,这个释义并不符合这里的场景。不知大家有没有用过 Flash,symbol 实际上就类似于 Flash 中的“影片剪辑”、或者“元件”。

因此我个人觉得,symbol 应该解释为“元件”最为恰当!

那 symbol 和 SVG Sprite 又有什么关系呢?

我们可以把 SVG 元素看成一个舞台,而 symbol 则是舞台上一个一个组装好的元件,这这些一个一个的元件就是我们即将使用的一个一个 SVG 图标。

<style>
.icon-box {
  width: 22px; height: 22px; cursor: pointer; display: inline-block;
}
.icon-box.svg-box:hover {
  color: tomato;
}
</style>
</head>
<body>
<svg style="display:none;width:0;height:0" width="0" height="0" focusable="false" aria-hidden="true">
  <symbol id="ic-zan" viewBox="0 0 1024 1024">
    <path d="M728.064 343.943529c-17.648941-2.891294-23.552-20.239059-26.503529-28.912941V104.026353C701.560471 46.200471 654.396235 0 595.425882 0c-53.007059 0-97.28 40.478118-106.134588 89.569882-29.997176 184.862118-138.541176 255.457882-217.630118 280.937412a26.142118 26.142118 0 0 0-18.130823 24.877177v560.067764c0 19.817412 16.022588 35.84 35.84 35.84h535.973647c56.018824-11.565176 94.328471-31.804235 120.892235-86.738823l120.832-416.105412c23.552-75.173647-14.757647-147.395765-100.231529-144.564706h-238.772706z m-571.813647 31.744H76.619294C35.358118 375.687529 0 410.383059 0 450.861176v462.426353c0 43.369412 32.406588 78.004706 76.619294 78.004706h79.631059c27.708235 0 50.115765-22.407529 50.115765-50.115764V425.863529a50.115765 50.115765 0 0 0-50.115765-50.115764z">
    </path>
  </symbol>
</svg>
<div class="wrap">
  <i class="icon-box svg-box">
    <svg width="90%" height="90%" fill="currentColor" aria-hidden="true" focusable="false" class="">
      <use xlink:href="#ic-zan"></use>
    </svg>
  </i>
</div>