Callback when CSS3 transition finishes

后端 未结 5 1905
野的像风
野的像风 2020-11-22 09:30

I\'d like to fade out an element (transitioning its opacity to 0) and then when finished remove the element from the DOM.

In jQuery this is straight forward since yo

5条回答
  •  眼角桃花
    2020-11-22 10:15

    The accepted answer currently fires twice for animations in Chrome. Presumably this is because it recognizes webkitAnimationEnd as well as animationEnd. The following will definitely only fires once:

    /* From Modernizr */
    function whichTransitionEvent(){
    
        var el = document.createElement('fakeelement');
        var transitions = {
            'animation':'animationend',
            'OAnimation':'oAnimationEnd',
            'MSAnimation':'MSAnimationEnd',
            'WebkitAnimation':'webkitAnimationEnd'
        };
    
        for(var t in transitions){
            if( transitions.hasOwnProperty(t) && el.style[t] !== undefined ){
                return transitions[t];
            }
        }
    }
    
    $("#elementToListenTo")
        .on(whichTransitionEvent(),
            function(e){
                console.log('Transition complete!  This is the callback!');
                $(this).off(e);
            });
    

提交回复
热议问题