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

本文介绍了如何编写兼容不同浏览器的animationend事件处理代码,通过检查浏览器前缀并结合Modernizr库,确保在各种浏览器中都能正确触发动画结束事件。示例代码展示了如何使用getComputedStyle和prefix()函数来适配webkit, o, ms和标准的animationend事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比如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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值