【发布时间】:2019-10-08 14:09:41
【问题描述】:
我想使用 CSS 关键帧在其容器中移动 SVG 元素。
如果我只有一个<circle />,我可以简单地在关键帧定义中使用cx / cy 属性。但是如果我有一个任意组(<g />)怎么办?一个组没有cx/cy,如果我想使用CSS'transform: translate(x,y),似乎我必须定义一个单元(如px)。
MWE(如何为bar 组设置动画?):
svg {
padding: 5px;
width: 150px;
height: 150px;
border: 1px solid #000;
}
.foo {
animation-duration: 3s;
animation-iteration-count: infinite;
animation-name: moveFoo;
}
.bar {
animation-duration: 3s;
animation-iteration-count: infinite;
animation-name: moveBar;
}
@keyframes moveFoo {
from {
cx: 10;
cy: 10;
}
to {
cx: 90;
cy: 90;
}
}
/* how to define this? */
@keyframes moveBar { }
<svg viewbox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle class="foo" r="5" fill="red" />
<g class="bar" transform="translate(90 10)">
<circle r="5" fill="blue" />
<text
y="1"
text-anchor="middle"
fill="white"
font-family="monospace"
font-size="5">
AB
</text>
</g>
</svg>
【问题讨论】: