Trigger CSS Animations in JavaScript

后端 未结 8 1179
粉色の甜心
粉色の甜心 2020-11-28 14:14

I don\'t know how to use JQuery, so I need a method which could trigger an animation using JavaScript only.

I need to call/trigger CSS Animation when the user scrol

8条回答
  •  庸人自扰
    2020-11-28 14:44

    Here's the main code:

    HTML:

    
    

    CSS:

    #myImg {
        //properties
        animation: animate 2s linear infinite //infinite is important!
    }
    @keyframes animate {
        //animation base
    }
    

    JS:

    document.getElementById("myImg").style.webkitAnimationPlayState = "paused";
    window.addEventListener("scroll", function() {
        document.getElementById("myImg").style.webkitAnimationPlayState = "running";
        setTimeout(function() {
            document.getElementById("myImg").style.webkitAnimationPlayState = "paused";
        }, 2000);
    });
    

提交回复
热议问题