javascript - 无法在 SVG g 元素上应用动画

标签 javascript html svg

我有以下 SVG文件。

        <svg
            xmlns:dc="http://purl.org/dc/elements/1.1/"
            xmlns:cc="http://creativecommons.org/ns#"
            xmlns:rdf="htntp://www.w3.org/1999/02/22-rdf-syntax-ns#"
            xmlns:svg="http://www.w3.org/2000/svg"
            xmlns="http://www.w3.org/2000/svg"
            version="1.1"
            width="1000"
            height="650"
            id="svgContainer">

            <g
                id="truck">
                <animate attributeName="fill" from="black" to="red" dur="5s"/>
                <path
                    d="m 655.589,484.36218 -6.18561,-128.61524 -110.99241,-15.79583 -34.55321,-87.58893 -94.74579,0 3.03024,178.75619 -322.238663,2.0203 0.145305,51.22351 z"
                    id="body"
                    fill="#000000"
                    stroke="#000000"
                    stroke-width="1px"
                    stroke-linecap="butt"
                    stroke-linejoin="miter"
                    stroke-opacity="1" />
                <animate attributeType="XML" attributeName="x" to="1000" begin="indefinite" dur="1s" />
                <animate attributeType="XML" attributeName="y" to="1000" begin="indefinite" dur="1s" />
            </g>
        </svg>
    </g>
</svg>

我只想用动画将它移动到其他地方,但它不起作用。我在这里缺少什么吗? (我想用里面的所有内容为 g 元素设置动画。为了简单起见,我删除了其余元素。)

最佳答案

OK,我这里改成了动画,如下。

<animateTransform
    attributeType="XML"
    attributeName="transform"
    type="translate"
    from="0,0" to="1000,1000"
    begin="0s" dur="1"
    repeatCount="indefinite"/>

它开始起作用了。

更新

我找到了更好的解决方案。在第一个中,动画后我的组元素返回到其原始位置。对于以下内容,它会保持原样。

<animateMotion
    from="0,0" to="500,0"
    dur="4s" fill="freeze"/>

关于javascript - 无法在 SVG g 元素上应用动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212296/

相关文章:

Javascript 无法识别 SWT BrowserFunction

javascript - 如何添加淡入淡出效果

html - CSS 布局问题 - 垂直对齐 : Bottom; and Scaling issues

添加到 ASP.NET Core 元素中的 site.css 文件时,CSS 不起作用。但在添加为内联时有效

ios - PhoneGap SVG 在 iOS 上作为 data-uri

javascript - 触发和捕获自定义事件

Javascript:在除一个 div 之外的任何 div 上 onmousedown 时运行函数

javascript - 为什么在 Chrome 上有效的合法 cors 请求在 FireFox 上会失败?

html - 如何使用:before for the third <li> element?

css - 为什么 SVG textPath 不渲染? [开发工具中的 0x0]