我想在嵌入式 SVG 中创建一个柱形图,在每列的鼠标悬停事件上使用简单的颜色动画效果。如何只创建一次动画并将其应用于任何列?
在下面的示例中,我将彩色动画放在第一列。具体问题是如何在不重复节点的情况下在其他列(节点)上应用相同的动画。我应该使用 JavaScript 吗?或者我可以在动画节点上使用某种引用吗?
<svg class="columnChart">
<g transform="matrix(1 0 0 -1 0 0) translate(0, -100)">
<rect width="10" height="100" x="0" y="0">
<animate attributeName="fill"
attributeType="XML"
begin="mouseover"
dur="0.25s"
fill="freeze"
to="#00ff00"/>
<animate attributeName="fill"
attributeType="XML"
begin="mouseout"
dur="0.25s"
fill="freeze"
to="#000000"/>
</rect>
<rect width="10" height="80" x="15" y="0"/>
<rect width="10" height="55" x="30" y="0"/>
<rect width="10" height="60" x="45" y="0"/>
</g>
<g transform="rotate(90)">
<text x="110">mo</text>
</g>
<g transform="rotate(90)">
<text x="110" y="-15">tu</text>
</g>
<g transform="rotate(90)">
<text x="110" y="-30">we</text>
</g>
<g transform="rotate(90)">
<text x="110" y="-45">th</text>
</g>
</svg>
提前感谢您的帮助。
最佳答案
不幸的是,您不能重复使用动画元素。所以是的,您可能应该以两种方式之一使用 Javascript。您可以使用它来复制动画元素并将它们添加到每个元素中,或者直接在 JS 中制作动画(无论如何可能效果会更好)。
关于javascript - 如何将相同的动画应用于多个 SVG 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819175/