javascript - 如何将相同的动画应用于多个 SVG 对象

标签 javascript html animation svg

我想在嵌入式 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/

相关文章:

javascript - 无法让 Sprite 在圆形路径中移动

Javascript 正则表达式 : if a letter is adjacent to a number, 添加下划线

javascript - 根据属性匹配两个单独的 Javascript 对象

javascript - 让 jQuery 动画函数无限循环?

javascript - 如何让点击div1时显示image1,点击div2时image2替换image1?

python - 使用多个文件时 Pystache html 字符被替换

javascript - 在 Ember JS 中返回当月的费用总额

javascript - 在 document.write() 之前获取文档 HTML;

ios - UIScrollview 动画取决于内容偏移量

java - Android - 显示/隐藏 ActionBar 项目时淡入/淡出