比如animationend就有幾種:webkitAnimationEnd/oAnimationEnd/MSAnimationEnd/animationend,這樣應(yīng)該怎么寫兼容的代碼?或者是優(yōu)雅降級的代碼比如requestAnimationFrame和cancelRequestAnimFrame可以這樣寫,但是animationend這樣寫不行...window.requestAnimationFrame=(function(){returnwindow.requestAnimationFrame||//Chromiumwindow.webkitRequestAnimationFrame||//Webkitwindow.mozRequestAnimationFrame||//MozillaGekowindow.oRequestAnimationFrame||//OperaPrestowindow.msRequestAnimationFrame||//IETrident?function(callback,element){//Fallbackfunctionconsole.log("Fallback");returnwindow.setTimeout(callback,1000/30);}})();window.cancelRequestAnimFrame=(function(){returnwindow.cancelAnimationFrame||window.webkitCancelRequestAnimationFrame||window.mozCancelRequestAnimationFrame||window.oCancelRequestAnimationFrame||window.msCancelRequestAnimationFrame||clearTimeout})();根據(jù)dolymood的方法,加上我wiki了一下,整理出一個可以判斷瀏覽器前綴的方法,當然,使用limichange提到的Modernizr.prefixed也很方便。varanimEndEventNames={'webkit':'webkitAnimationEnd','o':'oAnimationEnd','ms':'MSAnimationEnd','animation':'animationend'},animEndEventName=animEndEventNames[prefix().lowercase]||animEndEventNames['animation'];functionprefix(){varstyles=getCompStyle(document.documentElement),pre=(Array.prototype.slice.call(styles).join('').match(/-(moz|webkit|ms)-/)||['','o'])[1],dom=('WebKit|Moz|MS|O').match(newRegExp('('+pre+')','i'))[1];return{dom:dom,lowercase:pre,css:'-'+pre+'-',js:pre[0].toUpperCase()+pre.substr(1)};};functiongetCompStyle(elem,classes){return(window.getComputedStyle?window.getComputedStyle(elem,classes||null):elem.currentStyle)||null;}
js的html5方法有些有前綴,如何寫兼容的代碼?
哆啦的時光機
2019-04-08 09:21:57