1

So I have a piece of code. The purpose is to play a selected animation from Animate.css on click.

The code

  $(".container>parent").click(function () {
    $('.element').css({
      'animation': 'fadeInUp .2s',
      '-webkit-animation': 'fadeInUp .2s'
    });
  });

The problem

Animation runs, but only one time. 'Infinite' attribute causes chaos :P

What else could I do, to play that animation every single time someone click it?

Thanks for your time.

My HTML:

            <span class="parent">
                <img src="assets/myimage.png" class="filter-image">
                <span class="element">Text</span>
            </span>  

I want to animate the text everytime I click it.

5
  • Post your HTML, or make a jsfiddle Commented Apr 4, 2015 at 10:00
  • Propably because you didn't remove animation property after animation finishes Commented Apr 4, 2015 at 10:01
  • 1
    Hey, thanks and i am making a jsfiddle. But how to remove animation property after the animation finishes? Commented Apr 4, 2015 at 10:03
  • A better idea to me seems to add and remove a class and add it back again. Commented Apr 5, 2015 at 9:05
  • Thanks @BramVanroy for the suggestion, but the above way seems to work just fine. You said it for the sake of performance? Commented Apr 6, 2015 at 10:47

2 Answers 2

1
$(".container>parent").click(function() {
    $('.element').css({
        'animation': 'fadeInUp .2s',
        '-webkit-animation': 'fadeInUp .2s'
    });

    setTimeout(function(){
        $('.element').removeAttr('style');
    },300);
});
Sign up to request clarification or add additional context in comments.

1 Comment

You are the man! Totally understood my mistake. Case closed. Thank you and have a nice day. :)
0

The animation won't work the second time if you don't remove animation class after the current animation finishes.

But how to remove animation property after the animation finishes?

Here's a snippet:

var support = {};
support.animation = (function() {
    var animationEnd = (function() {
        var element = document.body || document.documentElement,
            animEndEventNames = {
                WebkitAnimation : 'webkitAnimationEnd',
                MozAnimation    : 'animationend',
                OAnimation      : 'oAnimationEnd oanimationend',
                animation       : 'animationend'
            }, name;

        for (name in animEndEventNames) {
            if (element.style[name] !== undefined) return animEndEventNames[name];
        }
    }());

    return animationEnd ? { end: animationEnd } : false;
})();



function animate(elem, cls, callback) {
    var $elem = $(elem);

    var onEndCallbackFn = function(ev) {
        if (support.animation) {
            $elem.removeClass(cls);
            this.removeEventListener(support.animation.end, onEndCallbackFn);
        }

        if (callback && typeof callback === 'function') { callback.call(this, ev); }
    };

    if (support.animation) {
        $elem.addClass(cls);
        $elem[0].addEventListener(support.animation.end, onEndCallbackFn);
    } else {
        onEndCallbackFn();
    }
}

usage is simple, just call animate function, like this:

animate($('.selector'), 'classWithAnimation', callbackFn);

In you case, you said you are using animate.css library:

$(".container>parent").click(function() {
    animate($('.element'), 'animated fadeInUp', function() {
        console.log('animation complete');
    );
});

Live example: jsFiddle

Comments

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.