不确定我在这里做错了什么......
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 :(得分:21)
好的,如果我弄错了,请帮助我 - 你的问题是你在animate方法中失去了对this
的引用?即你不能调用this.move()
或增加增量?
若是,请尝试 -
var requestId = requestAnimFrame(this.animate.bind(this));
请参阅有关使用requestAnimation回调here进行绑定的答案。
更新于2019年5月
如果你可以使用ES6,你可以使用箭头功能,它将保持这样的范围:
let requestId = requestAnimFrame(() => { this.animate(); });
在这里阅读箭头功能: