CSS3 transition events

前端 未结 6 1352
情歌与酒
情歌与酒 2020-11-22 07:18

Are there any events fired by an element to check wether a css3 transition has started or end?

6条回答
  •  臣服心动
    2020-11-22 07:47

    If you simply want to detect only a single transition end, without using any JS framework here's a little convenient utility function:

    function once = function(object,event,callback){
        var handle={};
    
        var eventNames=event.split(" ");
    
        var cbWrapper=function(){
            eventNames.forEach(function(e){
                object.removeEventListener(e,cbWrapper, false );
            });
            callback.apply(this,arguments);
        };
    
        eventNames.forEach(function(e){
            object.addEventListener(e,cbWrapper,false);
        });
    
        handle.cancel=function(){
            eventNames.forEach(function(e){
                object.removeEventListener(e,cbWrapper, false );
            });
        };
    
        return handle;
    };
    

    Usage:

    var handler = once(document.querySelector('#myElement'), 'transitionend', function(){
       //do something
    });
    

    then if you wish to cancel at some point you can still do it with

    handler.cancel();
    

    It's good for other event usages as well :)

提交回复
热议问题