无法在'Window'上执行'requestAnimationFrame':作为参数1提供的回调不是函数.

nat*_*ft1 7 javascript animation requestanimationframe

不知道我在这里做错了什么......

window.requestAnimFrame = function(){
return (
    window.requestAnimationFrame       || 
    window.webkitRequestAnimationFrame || 
    window.mozRequestAnimationFrame    || 
    window.oRequestAnimationFrame      || 
    window.msRequestAnimationFrame     || 
    function(/* function */ callback){
        window.setTimeout(callback, 1000 / 60);
    }
);
}();

function animationSequence(elem, ind) {
    this.ind = ind;
    this.elem = elem;
    this.distance = 450;
    this.duration = 900;
    this.increment = 0;
    this.start = Math.abs(this.ind)*450;
    var requestId = requestAnimFrame(this.animate);
    this.move();

    this.move = function() {
        this.elem.style.left = this.start - this.increment + "px";
    }
    this.animate = function() {
        var self = this;
        this.move();
        this.increment += 5;
        if (this.increment >= 450) { 
            if (this.ind == 0) { console.log("true"); this.elem.style.left = "1350px" }
            cancelAnimFrame(requestId);
        }
    }
    // this.animate();
}
Run Code Online (Sandbox Code Playgroud)

Bli*_*ill 22

好的,如果我弄错了,请帮帮我 - 你的问题是你this在animate方法中失去了参考吗?即你不能调用this.move()或增加增量?

若是,试试这个 -

 var requestId = requestAnimFrame(this.animate.bind(this));
Run Code Online (Sandbox Code Playgroud)

看到这个答案关于与requestAnimation回调结合在这里.

这篇关于绑定的博客文章.