Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Apply Style on CSS Animation End

How would I use jQuery to determine when a CSS animation for an element has ended and then apply a new style to that element? Here is my non-functional code below.

$("#icon").jQuery.Event("webkitAnimationEnd", function(event){
    ('#icon').css('opacity', '1');
}); 
like image 437
Tim Avatar asked Jun 05 '11 00:06

Tim


People also ask

What is the animationend event in CSS?

The animationend event occurs when a CSS animation has completed. For more information about CSS Animations, see our tutorial on CSS3 Animations. The numbers in the table specify the first browser version that fully supports the event. Numbers followed by "webkit" or "moz" specify the first version that worked with a prefix.

How do you animate in jQuery?

jQuery Animations - The animate() Method. The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated. The optional speed parameter specifies the duration of the effect.

What is animate () function in CSS?

A function that is called once the animation on an element is complete. The .animate () method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties.

How to manipulate CSS transitions and animations with JavaScript?

transitionend and its related events are quite helpful when manipulating CSS transitions and animations using JavaScript. Changing a CSS animation from its current values can be done by obtaining the stylesheets in JavaScript, but can be quite involved. In JavaScript, CSS transitions are generally easier to work with than CSS animations.


2 Answers

I think actually what you are looking for is the .bind() method:

$("#icon").addClass('thisClassWillApplyMyCSSAnimation').bind('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e){
    //DO WHATEV
    $('#icon').css('opacity', '1');
}); 
like image 137
Adam Coulombe Avatar answered Oct 27 '22 00:10

Adam Coulombe


You can use one()

$("#icon").one('animationend webkitAnimationEnd MSAnimationEnd oAnimationEnd', function(e) {
    //Your css
    $('#icon').css('opacity', '1');
}); 
like image 21
Ouadie Avatar answered Oct 26 '22 23:10

Ouadie