JavaScript探測CSS動畫是否已經完成的方法_javascript技巧

來源:互聯網
上載者:User

不囉嗦上代碼:

WN:(function(){      var el = $('<fakeelement>'),        transition="transition",        transitionEnd,        animEvent={'start':null,'iteration':null,'end':null},        vendorPrefix;              transition = transition.charAt(0).toUpperCase() + transition.substr(1);            vendorPrefix=(function(){//現在的opera也是webkit        var i=0, vendor=["Moz", "Webkit", "Khtml", "O", "ms"];        while (i < vendor.length) {          if (typeof el.css(vendor[i] + transition) === "string"){            return vendor[i];          }          i++;        }        return false;      })();            transitionEnd=(function(){        var transEndEventNames = {          WebkitTransition : 'webkitTransitionEnd',          MozTransition  : 'transitionend',          OTransition   : 'oTransitionEnd otransitionend',          transition    : 'transitionend'        }        for(var name in transEndEventNames){          if(typeof el.css(name) === "string"){            return transEndEventNames[name];          }        }      })();            animEvent.end=(function(){        var animEndEventNames = {          WebkitAnimation : 'webkitAnimationEnd',          animation   : 'animationend'        }        for(var name in animEndEventNames){          if(typeof el.css(name) === "string"){            return animEndEventNames[name];          }        }      })();            animEvent.iteration=(function(){        var animIterationEventNames = {          WebkitAnimation : 'webkitAnimationIteration',          animation   : 'animationiteration'        }        for(var name in animIterationEventNames){          if(typeof el.css(name) === "string"){            return animIterationEventNames[name];          }        }      })();            animEvent.start=(function(){        var animStartEventNames = {          WebkitAnimation : 'webkitAnimationStart',          animation   : 'animationstart'        }        for(var name in animStartEventNames){          if(typeof el.css(name) === "string"){            return animStartEventNames[name];          }        }      })();            return {        called:false,        addTranEvent:function(elem,fn,duration){          var self = this;          var fncallback = function(){            if(!self.called){              fn();              self.called = true;            }          };          function hand(){              elem.on(transitionEnd,function(){              //elem.unbind(transitionEnd,arguments.callee);              fncallback();            });          }          setTimeout(hand,duration);        },        addAnimEvent:function(elem,name,fn){          elem.on(animEvent[name],fn);        },              removeAnimEvent:function(elem,name,fn){          elem.unbind(animEvent[name],fn);        },        setStyleAttribute:function(elem,val){          if(Object.prototype.toString.call(val) === "[object Object]"){            for(var name in val){              if(/^transition|animation|transform/.test(name)){                var styleName=name.charAt(0).toUpperCase() + name.substr(1);                elem.css(vendorPrefix+styleName,val[name]);              }else{                elem.css(name,val[name]);              }            }          }        }      };    })(),

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.