requestAnimationFrame兼容性写法

Posted huang-gua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了requestAnimationFrame兼容性写法相关的知识,希望对你有一定的参考价值。

(function() {
    var lastTime = 0;
    var vendors = [‘webkit‘, ‘moz‘];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+‘RequestAnimationFrame‘];
        window.cancelAnimationFrame =
          window[vendors[x]+‘CancelAnimationFrame‘] || window[vendors[x]+‘CancelRequestAnimationFrame‘];
    }

    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); },
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };

    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());

  

以上是关于requestAnimationFrame兼容性写法的主要内容,如果未能解决你的问题,请参考以下文章

requestAnimationFrame 兼容不同浏览器

requestAnimationFrame

requestAnimationFrame 兼容方案

移动端滑屏全应用requestAnimationFrame的兼容与使用

requestAnimationFrame,Web中写动画的另一种选择

requestAnimationFrame,Web中写动画的另一种选择