作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
不确定我在这里做错了什么......
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();
}
最佳答案
好的,如果我误会了,请帮帮我——你的问题是你在 animate 方法中丢失了对 this
的引用吗?也就是说,您不能调用 this.move()
或增加 increment?
如果是这样试试这个-
var requestId = requestAnimFrame(this.animate.bind(this));
请参阅有关与 requestAnimation 回调绑定(bind)的答案 here .
还有这个blog post on binding .
2019 年 5 月更新
如果你可以使用 ES6,你可以使用箭头函数,它将保持这样的范围:
let requestId = requestAnimFrame(() => { this.animate(); });
在这里阅读箭头函数:
关于javascript - 无法在 'requestAnimationFrame' : The callback provided as parameter 1 is not a function. 上执行 'Window',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039180/
我是一名优秀的程序员,十分优秀!