首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用作背景图像时,SVG内部的Web动画中断

当用作背景图像时,SVG内部的Web动画中断
EN

Stack Overflow用户
提问于 2016-07-29 16:56:16
回答 1查看 311关注 0票数 1

我只是用在线服务为自己生成了一个基于SVG的加载指示器,但是每次使用它的页面加载时,我都会收到Chrome的警告,它告诉我SMIL动画受到了反对,这是相当令人讨厌的。为了摆脱它,我决定考虑用网络动画代替<animate>标签。下面是原始SVG:

代码语言:javascript
复制
<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>

我的结局是:

代码语言:javascript
复制
<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>

我要为我的成功而感到非常兴奋,当我注意到在CSS中用作background-image时,这个完全相同的SVG完全拒绝动画(请注意,我在这里使用了一个数据URI,但是当SVG使用常规的URL加载时,同样的情况也会发生)。

代码语言:javascript
复制
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中工作呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-29 20:51:41

不幸的是,目前还不支持带有backgrounImage属性的动画。

虽然CSS背景和边界模块3级编辑器的草稿说“动画:没有”的背景图像在撰写时,支持交叉褪色图像在CSS中出现在Chrome 19金丝雀。在广泛的支持到来之前,这可以通过图像、精灵和背景位置或不透明来伪造。要动画渐变,它们必须是相同的类型。

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

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

properties

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38663935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档