码迷,mamicode.com
首页 > 其他好文 > 详细

requestAnimationFrame兼容性扩展

时间:2015-12-03 23:11:19      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

/**
     * requestAnimationFrame兼容性扩展,两方面工作:
     * 1、把各浏览器前缀进行统一
     * 2、在浏览器没有requestAnimationFrame方法时将其指向setTimeout方法
     * */
    (function() {
        var lastTime = 0;
        var vendors = ["webkit", "moz"];
        for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
            window.requestAnimationFrame = window[vendors[x] + "RequestAnimationFrame"];
            // Webkit中此取消方法的名字变了
            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.7 - (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兼容性扩展

标签:

原文地址:http://www.cnblogs.com/laixiangran/p/5017638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!