题目描述
如何实现下图svg动画
相关代码
我只能实现出平面效果,想不出解决方案了
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-400 -400 800 800">
<g id='circle'>
<!--右边的圆-->
<circle id="right" cx="50" cy="0" r="50" fill="rgb(102,85,204)">
<!--右圆动画-->
<animate
id="right_go_left"
attributeName="cx"
attributeType="XML"
begin="0; right_go_right.end"
dur="0.5s"
from="50"
to="-50"
/>
<animate id="right_go_right"
attributeName="cx"
attributeType="XML"
begin="right_go_left.end"
dur="0.5s"
from="-50"
to="50"
/>
</circle>
<!--左边的圆-->
<circle id='left' cx="-50" cy="0" r="50" fill="rgb(255,0,153)">
<!--左圆动画-->
<animate
id="left_go_right"
attributeName="cx"
attributeType="XML"
begin="0; left_go_left.end"
dur="0.5s"
from="-50"
to="50"
/>
<animate
id="left_go_left"
attributeName="cx"
attributeType="XML"
begin="left_go_right.end"
dur="0.5s"
from="50"
to="-50"
/>
</circle>
</g>
</svg>
svg 的层叠关系由元素先后定义并加入决定的
如果要调整层叠关系,使用insertBefore并不能起到效果
所以要用占坑法
定义一些多余的 g 标签在需要动态调整层级的位置
然后动态地加入
起到覆盖与被覆盖的作用
动态移动两个元素的 顺序