3

I'm using animate.css for doing some animations. If an error occurs, the element should bounce:

$target.addClass('animated bounce');

That's working well. But if the user is doing the same thing, there won't be a second animation, as the classes are already added.

So I try to remove the classes after the animation like this:

$target.addClass('animated bounce');
setTimeout(function(){
    $target.removeClass('animated bounce');
}, 1000);

My question is, if this is how it should be done (I don't know how long the animation is exactly), or is there another solution for repeating animations?

1
  • Not sure I follow you 100%, but if using animate.css, to repeat animations, you can add the following CSS to force repeats: #yourElement { -vendor-animation-iteration-count: infinite; (use a number here if you you want to set a specif number of repeat animations) } Or, you can just set it like this if you want infinite repetitions: <div class="animated bounce infinite"></div> Where -vendor- is the vendor prefixes Commented Dec 7, 2015 at 20:06

3 Answers 3

4

You need to do this:

$('#target').removeClass().addClass('bounce animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
    $(this).removeClass();
});

$('#target') is in this case the ID of your element, change this to your needs.

Sign up to request clarification or add additional context in comments.

5 Comments

'one' should be 'on'? And 'oanimationend' -> 'onanimationend'?
No you just use it as it is in your error event. This small script will launch every time the error occurs. This is also advised by the maker of animate.css. You only need to change the target id.
Ok, but do I have to use one() as the classes will always be removed before they are added...?
Yes you must use .one(). This is a jquery event. whit this event you are actually calling the animate.css custom events. You don't need any Timeout().
@user3142695 If this is working for you please consider to up voting my answer
1

there is a shorter solution , you have to 'refresh' the animated element each time

function refreshElement(id){
   var el = $(id);
   el.before( el.clone(true) ).remove();
}

and that way you can run an animation on that element again

Comments

0

Run on mouse enter AND exit:

 $target.stop().toggleClass('animated bounce');

the animation class is added & removed based on the mouse's hover (enter/exit) action. The class will not be left active after exiting the item.

The .stop() will stop the animation from repeating multiple times if someone hovers/exits many times before an animation is complete.

3 Comments

Can you please explain what fix does?
@Phani the animation class is added & removed based on the mouse's hover (enter/exit) action. The class will not be left active after exiting the item.
If you can add the same on answer, it will be much more clear. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.