【问题标题】:SVG animation delay on each repetition每次重复的 SVG 动画延迟
【发布时间】:2015-10-19 20:51:36
【问题描述】:

我想为 SVG 动画循环的每次迭代添加延迟。这是一个简单的例子。

<svg xmlns="http://www.w3.org/2000/svg" width="100px" height="100px">
  <circle cx="50" cy="50" r="15" fill="blue">
    <animate id="op" attributeType="CSS" attributeName="opacity"
             from="1" to="0" dur="3s" repeatCount="indefinite" />
  </circle>
</svg>

使用begin只会延迟第一次迭代,那么有没有办法延迟每一次次迭代?

【问题讨论】:

标签: html svg svg-animate


【解决方案1】:

您可以将 SMIL 动画元素的 end 事件添加到 begin 属性。
此外,您可以向此 begin 属性添加多个值,以 ; 分隔:

<svg xmlns="http://www.w3.org/2000/svg" width="300px" height="100px">
  <circle cx="50" cy="50" r="15" fill="blue">
    <animate id="op" attributeType="CSS" attributeName="opacity"
             from="1" to="0" dur="3s" begin="3s;op.end+3s" />
  </circle>
</svg>

【讨论】:

  • 太棒了,谢谢!一开始我很困惑,因为我没有意识到这不适用于repeatCount
  • 实际上repeatCount 仍然处于活动状态,end 事件在有限 repeatCount 结束时触发。
  • 我想我应该更具体地说,如果你有repeatCount="indefinite",动画永远不会结束-永远不会到达end事件!我没有在 MDN 上看到具体提及默认值,但如果未定义,我假设它设置为 1。
  • 实际上,如果设置了持续时间,它会被忽略:specs 但如果 repeatCount 设置为 indefiniteend 永远不会触发。这就是为什么我说“在有限 repeatCount的末尾”
  • 您好 Kaiido,我在您的示例中没有看到重复计数,为什么动画会重新开始?在我的情况下,动画没有。
【解决方案2】:

定义虚拟循环并设置相对开始时间。看 How to make SVG Loop Animation?

<svg xmlns="http://www.w3.org/2000/svg" width="300px" height="200px">
  <rect>
    <animate id="o1" begin="0;o1.end" dur="10s"
    attributeName="visibility" from="hide" to="hide"/>
  </rect>
  <circle fill="orange" cx="-50" cy="100" r="20">
    <animate begin="o1.begin" 
    attributeName="cx" from="250" to="50" dur="5.05s"/>
  </circle>
  <circle fill="blue" cx="150" cy="100" r="50" />
  <circle fill="orange" cx="-50" cy="100" r="20">
    <animate begin="o1.begin+5s" 
    attributeName="cx" from="50" to="250" dur="5.05s"/>
  </circle>
</svg>

【讨论】:

    【解决方案3】:

    下面是一个“闭眼”的例子……感谢这个帖子中的建议。

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 80 16"><g>
      <g >
      <ellipse cx="9.45" cy="7.7" rx="0.96" ry="0.96" style="stroke: none; fill: black;">
       <animate id="op" attributeName="ry" attributeType="XML"
                 to="0.1"
                 begin="3s;op.end+3s" dur="0.15s"
                 fill="remove" repeatCount="2"
                />
      </ellipse>
      <ellipse cx="14.6" cy="7.8" rx="0.8" ry="0.8" style="stroke: none; fill: black;">
       <animate id="op" attributeName="ry" attributeType="XML"
                 to="0.1"
                 begin="3s;op.end+3s" dur="0.15s"
                 fill="remove" repeatCount="2"
                />
      </ellipse>
     </g>
    </svg>

    【讨论】:

      【解决方案4】:

      我认为您正在寻找的是 svg 的附加/累积属性。这是我从css tricks得到的一个例子

      svg {
        border: 3px solid #eee;
        display: block;
        margin: 1em auto;
      }
      <svg width="500" height="100">
        <circle id="orange-circle" r="30" cx="50" cy="50" fill="orange" />
      
        <animate xlink:href="#orange-circle" attributeName="cx" from="0" to="100" additive="sum" repeatCount="3" calcMode="spline" keyTimes="0;1" keySplines=".42 0 1 1" dur="1s" begin="click" fill="freeze" />
      </svg>

      其实这里有一个更好的例子(相同的来源)

      svg {
        border: 3px solid #eee;
        display: block;
        margin: 1em auto;
      }
      <svg width="500" height="150">
        <style>
          rect {
            -moz-transform-origin: 75px 75px;
            transform-origin: 50% 50%;
          }
        </style>
        <rect id="deepPink-rectangle" width="50" height="50" x="50" y="50" fill="deepPink" />
        
        <animateTransform 
                 xlink:href="#deepPink-rectangle"
                 attributeName="transform" 
                 attributeType="XML"
                 type="rotate"
                 from="0 75 75"
                 to="360 75 75" 
                 dur="2s"
                 begin="0s; 5s; 9s; 17s;"
                 end="2s; 8s; 15s; 25s;"
                 fill="freeze" 
                 restart="whenNotActive"
                 />
        
      </svg>

      【讨论】:

      • 感谢您的回答,但我认为这并不能具体解决问题。我需要延迟无限次重复,而这些示例似乎是有限的。
      【解决方案5】:

      这是 Danjiro Daiwa 示例的更详细版本,没有隐藏矩形来同步动画,也没有将橙色圆圈设置在可见区域之外来隐藏它们,而是使用opacity="0"。有四个动画同时运行,a1/a2 用于移动,o1/o2 用于隐藏橙色分身圆圈,当它们在蓝色圆圈后面/上方移动时,r1-r4 用于更改半径r 和 f1 -f4 改变fill的颜色。

      <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="200px">
        <circle fill="#f70" cx="0" cy="100" r="20" opacity="0">
          <animate id="a1" attributeName="cx" begin="0s;a2.end" from="250" to="50" dur="3s"/>
          <animate id="o1" attributeName="opacity" begin="a1.begin" end="a1.end" from="1" to="1"/>
          <animate id="r1" attributeName="r" begin="a1.begin" from="20" to="15" dur="1.5s"/>
          <animate id="r2" attributeName="r" begin="r1.end" from="15" to="20" dur="1.5s"/>
          <animate id="f1" attributeName="fill" begin="a1.begin" from="#f70" to="#c00" dur="1.5s"/>
          <animate id="f2" attributeName="fill" begin="f1.end" from="#c00" to="#f70" dur="1.5s"/>
        </circle>
      
        <circle fill="blue" cx="150" cy="100" r="50" />
      
        <circle fill="#f90" cx="0" cy="100" r="20" opacity="0">
          <animate id="a2" attributeName="cx" begin="a1.end" from="50" to="250" dur="2s"/>
          <animate id="o2" attributeName="opacity" begin="a2.begin" end="a2.end" from="1" to="1"/>
          <animate id="r3" attributeName="r" begin="a2.begin" from="20" to="25" dur="1s"/>
          <animate id="r4" attributeName="r" begin="r3.end" from="25" to="20" dur="1s"/>
          <animate id="f3" attributeName="fill" begin="a2.begin" from="#f70" to="#ff0" dur="1s"/>
          <animate id="f4" attributeName="fill" begin="f3.end" from="#ff0" to="#f70" dur="1s"/>
        </circle>
      </svg>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 2018-12-21
        • 2017-12-04
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多