2014-02-26 25 views
5

不知道我在做什麼錯在這裏...未能在'Window'上執行'requestAnimationFrame':作爲參數1提供的回調不是函數。

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(); 
} 
+0

是否嘗試在'this.animate'函數定義下放置'var requestId = requestAnimFrame(this.animate);'? – matewka

回答

15

好了,所以幫助我,如果我讓你錯了 - 是你的問題,你有動畫的方法中失去了你的參考this?即您不能撥打this.move()或增加增量?

如果是這樣的嘗試這 -

var requestId = requestAnimFrame(this.animate.bind(this)); 

查看關於與requestAnimation回調here結合這個答案。

而這個blog post on binding

+0

這個解決方案爲我工作 – Dustin

+0

@BlinkingCahill感謝隊友,它也爲我工作! –

+0

你救了我的同伴。 :) –

相關問題