Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run animation every 5 seconds/interval

I would like to shake this text every 5 seconds, so I tried to implement an animation in CSS and set an interval via jQuery, but it seems something is wrong...and ideas what's missing? Here's a fiddle: https://jsfiddle.net/s909gu2s/1/

.shk {
    transform: translate3d(0, 0, 0);
    backface-visibility: hidden;
    -webkit-animation-name: shake;
    -webkit-animation-duration: 1s;
    -webkit-animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;
    -webkit-animation-delay: 0s;
}

@keyframes shakeMe {
    10%, 90% {
        transform: translate3d(-5px, 0, 0);
    }

    20%, 80% {
        transform: translate3d(5px, 0, 0);
    }

    30%, 50%, 70% {
        transform: translate3d(-5px, 0, 0);
    }

    40%, 60% {
        transform: translate3d(5px, 0, 0);
    }
}

$(document).ready(function() {
    setInterval(function() {
        $(".shk").css("animation", "shakeMe");
    }, 500);
});

<div class="shk">Shake me</div>
like image 767
DevJoe Avatar asked Nov 06 '17 11:11

DevJoe


People also ask

How do I change the duration of an animation?

Set the speed To run your animation effect at a faster or slower pace, change the Duration setting. On the slide, click the text or object that contains the animation effect that you want to set the speed for. On the Animations tab, in the Duration box, enter the number of seconds that you want the effect to run.

What do I need to repeat animation again and again?

Answer: Ctrl F6 is used to repeat the animation. Explanation: Animation is used to transform static objects into motion.

How do you run an animation infinite number of times?

You can use integer values to define a specific amount of times the animation will play. animation-iteration-count: infinite; By using the keyword infinite , the animation will play indefinitely.

How do I change the animation-iteration-count?

You can also specify the number of animation iterations using the animation shorthand CSS property to use fewer lines of code. For example, in the following code: animation: example 3s 2s 5 ease; … the value 5 sets the number of animation iterations.


1 Answers

You can do it with pure CSS, no JS/jQuery needed. In order to accomplish this, I set the animation-duration to 5 seconds, then multiplied all percentage values by 0.2 (because 1 second out of 5 seconds => 20%). Then translate back to 0px after the highest percentage value. And voilà, shaking every 5 seconds:

.shk {
    transform: translate3d(0, 0, 0);
    backface-visibility: hidden;
    animation-name: shakeMe;
    animation-duration: 5s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes shakeMe {
    2%, 18% {
        transform: translate3d(-5px, 0, 0);
    }

    4%, 16% {
        transform: translate3d(5px, 0, 0);
    }

    6%, 10%, 14% {
        transform: translate3d(-5px, 0, 0);
    }

    8%, 12% {
        transform: translate3d(5px, 0, 0);
    }
    
    18.1% {
        transform: translate3d(0px, 0, 0);
    }
}
<div class="shk">Shake me</div>
like image 97
Constantin Groß Avatar answered Oct 05 '22 21:10

Constantin Groß