Trigger CSS Animations in JavaScript

后端 未结 8 1139
粉色の甜心
粉色の甜心 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:38

    If you want Animations i recommend you create a CSS class which you toggle on a Condition whit JS: CSS

    .animation {
     animation: anim 2s ease infinite;
     transition: .2s
    }
    

    JS

    // Select your Element
    
    $element.document.querySelector(".yourElement");
    $element.addEventListner('click', () => {
     $element.classList.toggle("animation")
    })
    
    0 讨论(0)
  • 2020-11-28 14:40

    You could use CSS to hide the image / animation and show when the user scrolls. This would work like this:

    CSS:

    div {
        border: 1px solid black;
        width: 200px;
        height: 100px;
        overflow: scroll;
    }
    
    #demo{
        display: none;
    }
    

    HTML:

    <div id="myDIV"> </div>
    
    <div id="demo">
         <img src="earthlogo.gif" id="earthlogo" alt="Thanks for scrolling. Now you see me">
    </div>
    

    Your javascript just needs to include an eventListener to call the function which triggers the display of your animation.

    JS:

    document.getElementById("myDIV").addEventListener("scroll", start);
    
    function start() {
        document.getElementById('demo').style.display='block';
    }
    
    0 讨论(0)
  • 2020-11-28 14:44

    Here's the main code:

    HTML:

    <img id="myImg">
    

    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);
    });
    
    0 讨论(0)
  • 2020-11-28 14:48

    Vanilla JS version

    document.getElementById('logo').classList.add("anim");
    document.getElementById('earthlogo').classList.add("anim2");
    
    0 讨论(0)
  • 2020-11-28 14:49

    The simplest method to trigger CSS animations is by adding or removing a class - how to do this with pure Javascript you can read here:

    How do I add a class to a given element?

    If you DO use jQuery (which should really be easy to learn in basic usage) you do it simply with addClass / removeClass.

    All you have to do then is set a transition to a given element like this:

    .el {
        width:10px;
        transition: all 2s;
    }
    

    And then change its state if the element has a class:

    .el.addedclass {
        width:20px;
    }
    

    Note: This example was with transition. But for animations its the same: Just add the animation on the element which has a class on it.

    There is a similar question here: Trigger a CSS keyframe animation via scroll

    0 讨论(0)
  • 2020-11-28 14:56

    I have a similar problem. The best answer didn’t work for me, but when I added the delay it worked. The following is my solution.

    CSS

    .circle_ani1,
    .circle_ani2 {
        animation-duration: 1s;
        animation-iteration-count: 1;
    }
    
    .circle_ani1 {
        animation-name: circle1;
    }
    
    .circle_ani2 {
        animation-name: circle2;
    }
    

    JS

    let temp_circle1 = $('.TimeCountdown_circle1').removeClass('circle_ani1');
    let temp_circle2 = $('.TimeCountdown_circle2').removeClass('circle_ani2');
    window.setTimeout(function() {
        temp_circle1.addClass('circle_ani1');
        temp_circle2.addClass('circle_ani2');
    }, 50);
    
    0 讨论(0)
提交回复
热议问题