5

I created a rotation animation in css that takes 1s and has 'forwards' set with a class .comet . How can i have that animation start at every click on an exterior element?

 #carousel #big_bubble #cometa.rotation{animation:rotation 1s forwards; -moz-animation:rotation 1.5s forwards; -webkit-animation:rotation 1s forwards; -o-animation:rotation forwards 1s; }
    @-moz-keyframes rotation{
        from{transform:rotate(0deg);}
        to{transform:rotate(-360deg);}
 @-webkit-keyframes rotation{
        from{-webkit-transform:rotate(0deg);}
        to{-webkit-transform:rotate(-360deg);}
    }
2
  • Can you provide a FIDDLE link ? Commented Feb 25, 2013 at 12:13
  • i provided the animation on CSS. Commented Feb 25, 2013 at 12:32

4 Answers 4

8
$(function() {// Shorthand for $( document ).ready()
    $( ".trigger" ).click(function(e) {
        e.preventDefault();
        $(this).addClass( "animation" ).one("webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend", function(){
            $(this).removeClass( "animation" );
        });
    });
});

Resources:

  1. How to Capture CSS3 Animation Events in JavaScript
  2. jQuery .one()
Sign up to request clarification or add additional context in comments.

Comments

3

Not sure I understand you right. Anyway:

$('#button').click(function() {
    var el = $('#cometa').addClass('rotation');
    setTimeout(function() {
        el.removeClass('rotation');
    }, 1000);
});

http://jsfiddle.net/EPv3B/

1 Comment

We shared the aproximate same answer on aproximate same time. It works. I only had to add extra time for removing class - the animation didn't finished very smooth with only 1000ms. Thx for your contribution anyway :)
0

On every click of the a element you add the css class to the element . check this code .

if .comet is your css animation class then

$(document).ready(function(){
     $('#everybutton_element_id').click(function (e) {
            $(this).addClass('comet');
            $(this).delay(2000).removeClass('comet');
     });
});

repeat the same code for any no of elements on which you want to implement it .

4 Comments

but it adds the class and it stays there. at the second click the animation it doesn't take place.
do you have many buttons or just one button . on which you want to fire this css class .
i have edited my code above . use it now it will work fine . as you want it to work .
i provided the tab yesterday
0
$('body').on('click', '#next', function() {
     $("#cometa").addClass("rotation");
     setTimeout(function () {
    $("#cometa").removeClass('rotation');
}, 1500);
});

This works for my problem. I remove that class after animation-time is finished.

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.