Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i repeat an animation set with css, at every click using JQuery?

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);}
    }
like image 953
Dan Ovidiu Boncut Avatar asked Feb 25 '13 12:02

Dan Ovidiu Boncut


People also ask

How do you make an animation repeat in CSS?

The animation-iteration-count property in CSS is used to specify the number of times the animation will be repeated. It can specify as infinite to repeat the animation indefinitely.

How do you trigger an onclick animation?

On the Animations tab, in the Advanced Animation group, click Animation Pane. In the Animation Pane, select the animation that you want to trigger. In the Advanced Animation group, click Trigger, point to On Click of, and select the object that you want to trigger the animation.

Which CSS property is used to play animation again and again?

The animation-play-state CSS property sets whether an animation is running or paused.

Can the jQuery animate () method be used to animate any CSS property?

The animate() is an inbuilt method in jQuery which is used to change the state of the element with CSS style. This method can also be used to change the CSS property to create the animated effect for the selected element. Syntax: (selector).


2 Answers

$(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()
like image 152
Riccardo Volpe Avatar answered Oct 16 '22 21:10

Riccardo Volpe


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/

like image 26
dfsq Avatar answered Oct 16 '22 21:10

dfsq