javascript - 当用作背景图像时,SVG 中的 Web 动画会中断

标签 javascript css svg background-image web-animations

我刚刚使用在线服务为自己生成了一个基于 SVG 的加载指示器,但每次加载使用它的页面时,我都会收到来自 Chrome 的警告,通知我 SMIL 动画已被弃用,这非常令人讨厌。为了摆脱它,我决定考虑更换 <animate>带有网络动画的标签。以下是原始 SVG:

<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" class="uil-ring-alt">
  <circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
  <circle cx="50" cy="50" r="40" stroke="#808080" 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>

这是我最终得到的结果:

<svg width='200px' height='200px' xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink">
  <circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/>
  <circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/>
  <script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script>
  <script type="text/javascript"><![CDATA[
    var line = document.getElementById("line");
    line.animate(
      [
        {strokeDashoffset:0},
        {strokeDashoffset:502}
      ],
      { duration: 2000, iterations: Infinity }
    );
    line.animate(
      [
        {strokeDasharray:"150.6 100.4"},
        {strokeDasharray:"1 250"},
        {strokeDasharray:"150.6 100.4"}
      ],
      { duration: 2000, iterations: Infinity }
    );
  ]]></script>
</svg>

我正要为我设法使它工作而感到非常兴奋,然后当我注意到完全相同的 SVG 用作 background-image 时,我的笑容卡住在我的脸上。在 CSS 中,完全拒绝动画(下面的演示;请注意,我在这里使用数据 URI 内联了 SVG,但是当使用常规 URL 加载 SVG 时也会发生同样的情况)。

body:before {
  content: '';
  display: block;
  width: 200px;
  height: 200px;
  background: url('data:image/svg+xml;utf8,<svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" xmlns:xlink="http://www.w3.org/1999/xlink"><circle cx="50" cy="50" r="40" stroke="white" fill="none" stroke-width="10" stroke-linecap="round"/><circle cx="50" cy="50" r="40" stroke="#808080" fill="none" stroke-width="6" stroke-linecap="round" id="line"/><script type="text/javascript" xlink:href="https://cdn.rawgit.com/web-animations/web-animations-js/45d8e40300e82ff02ccfbbc78c89500de0f5616f/web-animations.min.js"></script><script type="text/javascript"><![CDATA[var line = document.getElementById("line");line.animate([{strokeDashoffset:0},{strokeDashoffset:502}],{ duration: 2000, iterations: Infinity });line.animate([{strokeDasharray:"150.6 100.4"},{strokeDasharray:"1 250"},{strokeDasharray:"150.6 100.4"}],{ duration: 2000, iterations: Infinity });]]></script></svg>') no-repeat center center;
  background-size: contain;
}

我应该忽略警告并继续使用 SMIL 还是有办法让 Web 动画在 SVG 中工作?

最佳答案

很遗憾,尚不支持具有 backgrounImage 属性的动画。

While CSS Backgrounds and Borders Module Level 3 Editor’s Draft says “Animatable: no” for background-image at the time of writing, support for crossfading images in CSS appeared in Chrome 19 Canary. Until widespread support arrives this can be faked via image sprites and background-position or opacity. To animate gradients they must be the same type

您可以在以下文章中阅读更多内容:

http://oli.jp/2010/css-animatable-properties/

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties

关于javascript - 当用作背景图像时,SVG 中的 Web 动画会中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663935/

相关文章:

java - 其他网站地址与 url 不匹配

javascript - 对 firebase currentUser 所做的更改只有在完全重新加载应用程序后才会反射(reflect)出来

css - 无法更改 Sharepoint 2013 中 asp 菜单上的字体颜色

html - 列表上方的菜单白线

jQuery:在 SVG tspan 中写入 html 符号

html - svg viewbox无法调整大小

javascript - d3.js - 当鼠标悬停在 SVG 容器上的这些元素上时,如何将光标设置为手?

php - 用于检测 AIM 状态的 javascript 或 PHP 选项

javascript - for循环内的函数调用打破了循环

html - 边框图像未在 safari 中显示