2

I'm trying to loop a CSS3 transition, the idea is to add a class to the element to start the transition and listen to the transitionend event, when the event is fired I remove the class and start again.

var transition = function () {
    // add class to start the transition
    flipper.addClass('flipped');

    flipper.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
        // remove class when transition is done
        flipper.removeClass('flipped');
        transition();
    });
}

Here is a fiddle with the code

The problem is that it only runs the first time, when the class is added a second time the event is never fired.

Is there any other way to loop a transition?

1
  • Whoever voted to close this question - please pay more attention in future. The problematic code is shown clearly above, and there is a link to show the problem at work. There's nothing wrong with this question. Commented Feb 6, 2014 at 14:59

1 Answer 1

5

The browser is not getting chance to remove the class before you add it again, so it's not resetting the style. Putting a 1ms delay in fixes it...

flipper.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function () {
    // remove class when transition is done
    $('#console').append('[removing class]');
    flipper.removeClass("flipped");
    setTimeout(function() {
        transition();
    }, 1);
});

Modified jsFiddle link

Note:

You could pass transition as a parameter to the timeout call, like this...

setTimeout(transition, 1);

It's just a habit of mine to do it as I have above.

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

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.