9

I have several CSS3 animations linked to one div, but I only want a function to be callad a the end of the last animation.

I have used the animationEnd event so that I can trigger said function but as I said I only want the it to run on the last animation.

Is there a way to detect with animation has ended by checking the name of the animation that has triggered the animationEnd event?

thus allowing me to use a if statement to single out the last animation.

Mike Eng
  • 1,593
  • 4
  • 34
  • 53
Damon
  • 115
  • 1
  • 1
  • 6

2 Answers2

8

The parameter is required when you define the function. Either of these should work;

var $element = $('.eye').bind("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend", function(event){
     if (event.originalEvent.animationName === "three") {
         console.log('the event happened');
     }
}

Or,

var $element = $('.eye').bind("webkitAnimationEnd oAnimationEnd msAnimationEnd animationend", function(e){
     if (e.originalEvent.animationName === "three") {
         console.log('the event happened');
     }
}
Mike Eng
  • 1,593
  • 4
  • 34
  • 53
Matt Clegg
  • 584
  • 1
  • 4
  • 16
5

I don't know why... but I can catch the the animationName only by e.originalEvent.animationName

so the best option is:

function getAnimationName(e) {
  if(e.animationName != undefined) return e.animationName;
  if(e.originalEvent.animationName != undefined) return e.originalEvent.animationName;
  else return undefined;
}
Almog Baku
  • 810
  • 1
  • 9
  • 22
  • 1
    I think if you're using jQuery, then the event is all jQuery-ified, so you have to access the original event to find the animationName property. – Eric Johnson Dec 15 '15 at 17:16