简体   繁体   English

CSS animation 的 jQuery 事件监听器结束

[英]jQuery event listener for CSS animation end

I have a problem with jQuery.我对 jQuery 有疑问。 I have tried adding the .on event listener for when an animation ends.我尝试添加.on event listener器,用于 animation 结束时。 Its not working.它不工作。 I want the jetPosition and animationDuration to be random every time the animation restarts.我希望每次 animation 重新启动时, jetPositionanimationDuration都是随机的。 I put a console.log to check if the listener is triggered and it is not:( Any help would be very much appreciated.我放了一个console.log来检查监听器是否被触发:(非常感谢任何帮助。

I also changed it on further research to animationiteration event listener.我还在进一步研究中将其更改为动画迭代事件侦听器。 It seems to work though it doesn't always start from the left of the screen and has spurts of the repeat in console log without clear reason.尽管它并不总是从屏幕左侧开始,并且在没有明确原因的情况下在控制台日志中重复出现,但它似乎可以工作。

As an extra bonus, I would love to apply multiple instances of jet all over the viewport.作为额外的奖励,我希望在整个视口中应用多个 jet 实例。

CSS: CSS:

#jet {
    animation: fly linear 0s infinite normal backwards;
}

/*Keyframes*/

@keyframes fly {
    from {
        transform: translateX(-10vw)
    }

    to {
        transform: translateX(140vw)
    }
}

jQuery: jQuery:

function getRndInteger(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    // Jet Animation
    var jet = $("#jet");

    jet.css({
        top: getRndInteger(20, 50) + "vh",
        left: '0vw',
        width: '8vw',
        position: 'absolute',
        animationDuration: getRndInteger(5, 10) + "s"
    });

    function flyJet() {
        jet.css({
            top: getRndInteger(20, 50) + "vh",
            left: '0vw',
            width: '8vw',
            position: 'absolute',
            animationDuration: getRndInteger(5, 10) + "s"
        });
    }


    jet.on("animationiteration", function () {
        console.log("repeating");
        flyJet();
    });

Try using something like:尝试使用类似的东西:

element.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',   
    function(e) {
    // code to execute after transition ends
});

Check out this for more info.查看以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM