CSS Animation onClick

前端 未结 8 370
忘了有多久
忘了有多久 2020-11-30 22:25

How can I get a CSS Animation to play with a JavaScript onClick? I currently have:

.classname {
  -webkit-animation-name: cssAnimation;
  -webkit-animation-d         


        
相关标签:
8条回答
  • 2020-11-30 23:15

    Try this:

    <div>
     <p onclick="startAnimation()">Start</p><!--O botão para iniciar (start)-->
     <div id="animation">Hello!</div> <!--O elemento que você quer animar-->
    </div>
    
    <style>
    @keyframes animationName {
    from {margin-left:-30%;}
    }
    </style>
    
    <script>
    function startAnimation() {
        document.getElementById("animation").style.animation = "animationName 2s linear 1";
    }
    </script>
    
    0 讨论(0)
  • 2020-11-30 23:19

    Add a

    -webkit-animation-play-state: paused;
    

    to your CSS file, then you can control whether the animation is running or not by using this JS line:

    document.getElementById("myDIV").style.WebkitAnimationPlayState = "running";
    

    if you want the animation to run once, every time you click. Remember to set

    -webkit-animation-iteration-count: 1;
    
    0 讨论(0)
提交回复
热议问题