Is there a feasible way to trigger CSS keyframe animation using JS?

前端 未结 3 1535
谎友^
谎友^ 2021-01-18 07:58

Naturally, we can create a CSS animation using keyframes, and control it from there.

However, ideally, I would like to trigger this animation from a button click - s

3条回答
  •  一生所求
    2021-01-18 08:22

    If you want to make the animation happen and always end before allowing the event listener to trigger it again, I would suggest to control the behaviour like this:

    // Add this to your event listener
    if (!element.classList.contains("myClass")) {
        element.className = "myClass";
        setTimeout(function() {
            element.classList.remove("myClass");
        }, 1000); //At least the time the animation lasts
    }
    

提交回复
热议问题