【问题标题】:How to animate SVG?如何为 SVG 制作动画?
【发布时间】:2018-04-24 16:27:16
【问题描述】:

我想使用 jQuery "animate"-call 来为 SVG 设置动画。

SVG 应该被旋转或缩放。


到目前为止我尝试过的是这个简单的代码,但显然到目前为止这不起作用:

$("#svg").animate({
  transform: "scale(0.4)"
}, 400)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200px" viewBox="0 0 1 1" style="fill:purple;stroke:red" id="svg">
<svg width="100%" height="100%">
    <svg width="100%" ID="piece" y="0" x="0" class="black" height="100%">
        <g transform="translate(0.005) scale(0.022)">
            <path d="M 22 9 C 19.792 9 18 10.792 18 13 C 18 13.885103 18.29397 14.712226 18.78125 15.375 C 16.829274 16.496917 15.5 18.588492 15.5 21 C 15.5 23.033947 16.442042 24.839082 17.90625 26.03125 C 14.907101 27.08912 10.5 31.578049 10.5 39.5 L 33.5 39.5 C 33.5 31.578049 29.092899 27.08912 26.09375 26.03125 C 27.557958 24.839082 28.5 23.033948 28.5 21 C 28.5 18.588492 27.170726 16.496917 25.21875 15.375 C 25.70603 14.712226 26 13.885103 26 13 C 26 10.792 24.208 9 22 9 z " />
        </g>
    </svg>

我还添加了这一行 $("#svg").css({transform: "scale(0.4)"}) 以尝试是否将 scale 样式应用于元素,但是 - 这是有效的


如何解决这个问题,为什么我无法为 SVG 元素设置动画?任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 欢迎来到 SO,在提问之前搜索总是一个好主意。
  • 我不相信您可以将animatetransform 一起使用。尝试指定一个新的widthheight,或者切换到CSS animations
  • @DarrenSweeney 这不是问题。正如 OP 所述,设置 transform:scale(0.4) 直接有效,这是有效的 CSS。但就像@Blazemonger 所说,jQuery .animate() 需要一个数字,而不是作为属性的函数。这是一个解决方法:stackoverflow.com/questions/36243887/…

标签: javascript jquery html css svg


【解决方案1】:

您希望它制作动画并缩小 svg,对吗? 这是一个有效的狙击手

$("#svg").animate({
  width: "10%", height: "10%"
}, 400)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200px" viewBox="0 0 1 1" style="fill:purple;stroke:red" id="svg">
<svg width="100%" height="100%">
    <svg width="100%" ID="piece" y="0" x="0" class="black" height="100%">
        <g transform="translate(0.005) scale(0.022)">
            <path d="M 22 9 C 19.792 9 18 10.792 18 13 C 18 13.885103 18.29397 14.712226 18.78125 15.375 C 16.829274 16.496917 15.5 18.588492 15.5 21 C 15.5 23.033947 16.442042 24.839082 17.90625 26.03125 C 14.907101 27.08912 10.5 31.578049 10.5 39.5 L 33.5 39.5 C 33.5 31.578049 29.092899 27.08912 26.09375 26.03125 C 27.557958 24.839082 28.5 23.033948 28.5 21 C 28.5 18.588492 27.170726 16.496917 25.21875 15.375 C 25.70603 14.712226 26 13.885103 26 13 C 26 10.792 24.208 9 22 9 z " />
        </g>
    </svg>

这里是使用 animate 函数的文档:here

或者用 CSS 来做

#svg {
  animation: reduce 2s 2; /* To make the animation twice */
}
@keyframes reduce {
    from {transform : scale(1);}
    to {transform : scale(0.4);}
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200px" viewBox="0 0 1 1" style="fill:purple;stroke:red" id="svg">
<svg width="100%" height="100%">
    <svg width="100%" ID="piece" y="0" x="0" class="black" height="100%">
        <g transform="translate(0.005) scale(0.022)">
            <path d="M 22 9 C 19.792 9 18 10.792 18 13 C 18 13.885103 18.29397 14.712226 18.78125 15.375 C 16.829274 16.496917 15.5 18.588492 15.5 21 C 15.5 23.033947 16.442042 24.839082 17.90625 26.03125 C 14.907101 27.08912 10.5 31.578049 10.5 39.5 L 33.5 39.5 C 33.5 31.578049 29.092899 27.08912 26.09375 26.03125 C 27.557958 24.839082 28.5 23.033948 28.5 21 C 28.5 18.588492 27.170726 16.496917 25.21875 15.375 C 25.70603 14.712226 26 13.885103 26 13 C 26 10.792 24.208 9 22 9 z " />
        </g>
    </svg>

我制作了两次动画,但使用 animation-iteration-count 属性做你想做的事:documentation here

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    你也可以在这里尝试 SVG Gator 制作的一个很好的网络工具: https://app.svgator.com/

    有一些动画资源。

    最好的问候。

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 2017-06-11
      • 1970-01-01
      • 2016-02-24
      • 2019-11-22
      • 2019-11-04
      • 2016-08-12
      • 1970-01-01
      • 2016-08-30
      相关资源
      最近更新 更多