【问题标题】:Why does the SVG animation stop when using xlink:href for external file为什么使用 xlink:href 处理外部文件时 SVG 动画停止
【发布时间】:2017-07-03 23:11:51
【问题描述】:

由于某种原因,我的 spinner.svg 在使用 xlink:href 时没有动画。将 SVG 嵌入页面并使用 xlink:href 似乎可以正常工作,如下面的 sn-p 所示。

问题:静态(和实心!)微调器而不是动画

为什么 SVG 的动画标签突然不生效了? 既然图片实际是显示的,那肯定是引用工作了。

编辑:这可能与影子 dom 和外部参照有关吗? According to Sara Soueidan "目标元素必须是当前 SVG 文档片段的一部分"。我可能会超载“文档片段”的含义,但对我来说,文档片段属于 Shadow DOM 领域,我怀疑在使用 <use> 语句时,SMIL 动画可能无法正常工作?

工作版本

.xlinked {
  color: green;
}

img {
  color: red; // not taking effect - of course! just a test.
}

.embedded {
  color: blue;
}
  
<h1>xlink local</h1>
<svg class="xlinked">
    <!-- could not get this external reference to work?
    <use xlink:href="http://imgh.us/spinner_1.svg" />
    -->
    <use xlink:href="#spinner" />
</svg>

<h1>embedded</h1>
<div class="embedded">
    <svg id="embedded"  xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
        <circle cx="50" cy="50" r="45" stroke="rgba(43,43,43,0.3)" fill="none" stroke-width="10" stroke-linecap="round"/>
        <circle cx="50" cy="50" r="45" stroke="currentColor" fill="none" stroke-width="6" stroke-linecap="round">
            <animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/>
            <animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/>
        </circle>
    </svg>
</div>

<h1>img</h1>
<img src="http://imgh.us/spinner_1.svg" />

<h1>External absolute xlink (not working)</h1>
<svg>
    <!-- could not get this external reference to work. should be the same as a local reference?  -->
    <use xlink:href="http://imgh.us/spinner_1.svg" />
</svg>

<h1>Source SVG with symbols for xlink reference </h1>
<svg xmlns="http://www.w3.org/2000/svg"><symbol id="spinner" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid"><circle cx="50" cy="50" r="45" stroke="rgba(43,43,43,0.3)" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="45" stroke="currentColor" fill="none" stroke-width="6" stroke-linecap="round"><animate attributeName="stroke-dashoffset" dur="2s" repeatCount="indefinite" from="0" to="502"/><animate attributeName="stroke-dasharray" dur="2s" repeatCount="indefinite" values="150.6 100.4;1 250;150.6 100.4"/></circle></symbol></svg>

【问题讨论】:

    标签: animation svg svg-animate smil xlink


    【解决方案1】:

    在 SVG1.1 中,使用元素需要引用 SVG 片段而不是 SVG URL。尝试将 id 属性添加到外部 SVG 文件的根 svg 元素,并将哈希字符串添加到 use 元素的 href 值,像这样。

    外部 svg(spinner_1.svg)

    <svg xmlns="http://www.w3.org/2000/svg" id="root">
    <!--svg structure-->
    </svg>
    

    html 使用外部 SVG 文件

    <svg>
        <use xlink:href="http://imgh.us/spinner_1.svg#root" />
    </svg>
    

    注意:在 SVG2 中,您可以将 SVG URL 设置为 use 元素的 href 属性。见https://www.w3.org/TR/2016/CR-SVG2-20160915/struct.html#UseElement

    【讨论】:

    • 虽然这解释了为什么您可以在 sn-p 中看到的直接外部引用不起作用,但它并没有解释主要问题,这就是动画不起作用的原因。正如您从图片中看到的,我将 svg 片段称为“(..)#spinner”。这可行,但动画“死”了。
    • 在问题中添加了来自 Sara Soueidan 的有关 SMIL 的一些信息。
    • @oligofren 但是动画元素至少有一个内部类型的 xlink:href 属性指向它们的目标。 w3.org/TR/2001/REC-smil-animation-20010904/…
    • @oligofren,是的,回答者可以编辑他的答案来说明这一点。注意:深入研究这个问题我发现如果目标元素已通过href 属性设置,FF 也不支持外部动画。此外,即使路径已链接,chrome 和 FF 都乐于为路径设置动画 (&lt;use&gt;&lt;animate&gt;&lt;/use&gt;) 但我想这对您没有帮助,因为您必须为每个目标使用一个 &lt;use&gt;...跨度>
    • @kaiido FF 51 应该正确地尊重 href。如果这没有发生 raise a bug in bugzilla 将您的测试用例附加到错误中。
    猜你喜欢
    • 2020-08-07
    • 1970-01-01
    • 2021-03-16
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多