Search code examples
jqueryhtmlwebkitcallbackwebkit-animation

jquery html5 webkit-animation callback


Is there any callback on webkit-animation complete? see example

@-webkit-keyframes "blink" {
    0% { opacity: 1; }
    100% { opacity: 0; }
}
.animate {
    background: #000;
    height: 100px;
    width: 100px;
    opacity: 0;
    -webkit-animation-direction: normal;
    -webkit-animation-duration: 2s;
    -webkit-animation-iteration-count: 1;
    -webkit-animation-name: blink;
    -webkit-animation-timing-function: ease;
}​

$("div").bind("webkitTransitionEnd", function() {
  alert(1);
}).addClass("animate");​

But this callback doesn't work


Solution

  • This will do the trick:

    element.addEventListener('webkitAnimationEnd', function(event) { });

    in firefox the event is called 'animationend', but some webkit browsers will listen to both of these. Instead what you can do if you use jquery is to use

    $element.on('webkitAnimationEnd animationend' , function(event){ });


    Update:

    I recently had a small mishap using .one('webkitAnimationEnd animationend') since both events are being listened to in chrome, but only one fired at a time, the same function will fire twice on two seperate animation end events.

    Instead a small trick could be using a function similar to this:

    getTransitionEndEvent : function(){
        switch(this._currentBrowser){
            case enums.Browser.SAFARI:
            case enums.Browser.CHROME:
                return "webkitTransitionEnd";
            case enums.Browser.FIREFOX:
                return "transitionend";
            default:
                console.log("unknown browser agent for transition end event");
                return "";
        }
    }
    

    and add more vendor specific prefixes as needed.

    In order to identify the browser I can really recommend this:

    How to detect Safari, Chrome, IE, Firefox and Opera browser?