SVG 动画 G 元素

Int*_*ive 4 css animation svg

我目前正在学习如何使用 CSS 为 svg 对象设置动画。

我现在知道如何为路径设置动画:

@keyframes stroke_fill {
    0% {
        fill: white;
    }
    50% {
        fill: white;
        stroke-dashoffset: 0;
    }
    100% {
        fill: black;
        stroke-dashoffset: 0;
    }
}

.animation{
    animation: stroke_fill 4s ease forwards;
    stroke-dasharray: 324.774;
    stroke-dashoffset: 324.774; 
}
Run Code Online (Sandbox Code Playgroud)

组合在一起的路径显示在<g>标签中。
是否可以为<g>标签设置动画?

如果所有孩子都有相同的动画和相同的时间,那就太好了。
现在,如果我运行一个复杂的 svg 文件,我必须为每条路径指定一个类,这将花费大量时间。

分组进行会节省很多时间。

这是一个小提琴:https : //jsfiddle.net/vvvwcrdy/

Pau*_*eau 6

对的,这是可能的。你试了吗?

演示:

@keyframes stroke_fill {
    0% {
        fill: white;
    }
    50% {
        fill: white;
        stroke-dashoffset: 0;
    }
    100% {
        fill: black;
        stroke-dashoffset: 0;
    }
}

.animation{
    animation: stroke_fill 4s ease forwards;
    stroke-dasharray: 324.774;
    stroke-dashoffset: 324.774; 
    stroke: red;
    stroke-width: 10;
}
Run Code Online (Sandbox Code Playgroud)
<svg>
  <g class="animation">
    <rect x="20" y="20" width="120" height="120" />
    <rect x="160" y="20" width="120" height="120" />
  </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

  • 是的。“&lt;g&gt;”元素表示逻辑分组。不是物理的。组是提供其所有后代继承的属性的一种方式。 (2认同)