在重复循环结束时修改 SVG animateTransform

发布于 2024-12-23 14:10:26 字数 1236 浏览 3 评论 0原文

我试图让对象随着时间的推移旋转得越来越快,但每次动画重复时都会遇到事件触发问题。 SVG/SMIL 的 repeatEvent 似乎是我所需要的,但是在以下代码中,onSpinEnd() 似乎根本没有被调用。我做错了什么?

    <path id='coloredPart' d='…'>
        <animateTransform
            id='spin'
            attributeName='transform'
            begin='0s'
            dur='5s'
            type='rotate'
            from='0 0 0'
            to='-360 0 0'
            repeatCount='indefinite'
        />
    </path>

    <script>  <![CDATA[
        var spin = document.getElementById('spin');
        var onSpinEnd = function() {
            var intPart = 0;
            intPart = parseInt(spin.getAttribute('dur')[0].slice(0, -1));
            console.log(intPart);
            if (intPart > 1) --intPart;
            spin.setAttribute('dur', intPart.toString() + 's');
        };
        spin.addEventListener('repeatEvent', onSpinEnd, false)
        ]]>   
    </script>

事实证明, [0].slice(0, -1)) 不会从“16s”中给出“16”; .slice(0, -1)) 会的。该行应为:

intPart = parseInt(spin.getAttribute('dur').slice(0, -1));

I'm trying to make an object rotate faster and faster as time goes on, and I'm having trouble having an event fire every time the animation repeats itself. SVG/SMIL's repeatEvent seems to be what I need, but in the following code, onSpinEnd() doesn't appear to be called at all. What am I doing wrong?

    <path id='coloredPart' d='…'>
        <animateTransform
            id='spin'
            attributeName='transform'
            begin='0s'
            dur='5s'
            type='rotate'
            from='0 0 0'
            to='-360 0 0'
            repeatCount='indefinite'
        />
    </path>

    <script>  <![CDATA[
        var spin = document.getElementById('spin');
        var onSpinEnd = function() {
            var intPart = 0;
            intPart = parseInt(spin.getAttribute('dur')[0].slice(0, -1));
            console.log(intPart);
            if (intPart > 1) --intPart;
            spin.setAttribute('dur', intPart.toString() + 's');
        };
        spin.addEventListener('repeatEvent', onSpinEnd, false)
        ]]>   
    </script>

As it turns out, [0].slice(0, -1)) won't give me "16" from "16s"; .slice(0, -1)) will. That line should read:

intPart = parseInt(spin.getAttribute('dur').slice(0, -1));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2024-12-30 14:10:27

我不确定这片是做什么用的。这似乎适用于 Firefox。

var onSpinEnd = function() {
    var spin = document.getElementById('spin');
    var intPart = 0;
    intPart = parseInt(spin.getAttribute('dur')[0]);
    console.log(intPart);
    if (intPart > 1) --intPart;
    spin.setAttribute('dur', intPart.toString() + 's');
};

此外,据我所知,目前只有 Opera 和 Firefox 会触发 SMIL 事件。

I'm not sure what the slice is for. This seems to work on Firefox.

var onSpinEnd = function() {
    var spin = document.getElementById('spin');
    var intPart = 0;
    intPart = parseInt(spin.getAttribute('dur')[0]);
    console.log(intPart);
    if (intPart > 1) --intPart;
    spin.setAttribute('dur', intPart.toString() + 's');
};

Additionally, as far as I know only Opera and Firefox currently fire SMIL events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文