html5页面开头如何写,js的html5方法有些有前缀,如何写兼容的代码?

比如animationend就有几种:webkitAnimationEnd/oAnimationEnd/MSAnimationEnd/animationend,这样应该怎么写兼容的代码?或者是优雅降级的代码

比如requestAnimationFrame和cancelRequestAnimFrame可以这样写,但是animationend这样写不行...

window.requestAnimationFrame = (function(){

return window.requestAnimationFrame || //Chromium

window.webkitRequestAnimationFrame || //Webkit

window.mozRequestAnimationFrame || //Mozilla Geko

window.oRequestAnimationFrame || //Opera Presto

window.msRequestAnimationFrame || //IE Trident?

function(callback, element){ //Fallback function

console.log("Fallback");

return window.setTimeout(callback, 1000/30);

}

})();

window.cancelRequestAnimFrame = ( function() {

return window.cancelAnimationFrame ||

window.webkitCancelRequestAnimationFrame||

window.mozCancelRequestAnimationFrame ||

window.oCancelRequestAnimationFrame ||

window.msCancelRequestAnimationFrame ||

clearTimeout

} )();

根据 dolymood 的方法,加上我wiki了一下,整理出一个可以判断浏览器前缀的方法,当然,使用limichange提到的Modernizr.prefixed也很方便。

var animEndEventNames = {

'webkit' : 'webkitAnimationEnd',

'o' : 'oAnimationEnd',

'ms' : 'MSAnimationEnd',

'animation' : 'animationend'

},

animEndEventName = animEndEventNames[prefix().lowercase]||animEndEventNames['animation'];

function prefix(){

var styles = getCompStyle(document.documentElement),

pre = (Array.prototype.slice.call(styles).join('')

.match(/-(moz|webkit|ms)-/) || ['', 'o']

)[1],

dom = ('WebKit|Moz|MS|O').match(new RegExp('(' + pre + ')', 'i'))[1];

return {

dom: dom,

lowercase: pre,

css: '-' + pre + '-',

js: pre[0].toUpperCase() + pre.substr(1)

};

};

function getCompStyle(elem,classes){

return (window.getComputedStyle?window.getComputedStyle(elem,classes||null):elem.currentStyle) || null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值