检查供应商的正确方法 例如 webkit请求动画帧 ||mozRequestAnimationFrame.

Correct way to check for vendors Ex. webkitRequestAnimationFrame || mozRequestAnimationFrame

本文关键字:请求 动画 webkit mozRequestAnimationFrame 例如 供应商 方法 检查      更新时间:2023-09-26

我从谷歌(https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery)那里得到了这段代码:

var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;

这怎么有效?如果未定义 requestAnimationFrame,javascript 将在检查 moz、webkit 或 ms 之前崩溃。

不应该是:

var raf =   
typeof requestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof mozRequestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof webkitRequestAnimationFrame !== 'undefined' ? requestAnimationFrame :
typeof msRequestAnimationFrame!== 'undefined' ? requestAnimationFrame : null;

我看到的方法是在每个变量之前加上前缀window.。 如果您尝试访问未定义的变量,但不访问对象上的未定义属性,JavaScript 将引发错误。

var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;