Javascript 未能执行';requestAnimationFrame';在';窗口';:作为参数1提供的回调不是函数。
不知道我做错了什么Javascript 未能执行';requestAnimationFrame';在';窗口';:作为参数1提供的回调不是函数。,javascript,animation,requestanimationframe,Javascript,Animation,Requestanimationframe,不知道我做错了什么 window.requestAnimFrame = function(){ return ( window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestA
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();
}
好的,如果我理解错了,请帮助我-您的问题是您在动画方法中丢失了对
此
的引用吗?i、 e.您不能调用this.move()
或增加增量
如果是这样,试试这个-
var requestId = requestAnimFrame(this.animate.bind(this));
请参阅关于使用requestAnimation回调绑定的回答
还有这个
2019年5月更新
如果可以使用ES6,则可以使用箭头函数,该函数将保持如下范围:
let requestId = requestAnimFrame(() => { this.animate(); });
在此处阅读有关箭头函数的信息:
您是否尝试放置
var requestId=requestAnimFrame(this.animate)代码>下面的这个。动画
函数定义?@BlinkingCahill谢谢你,伙计,它对我也有用!你救了我的同伙。:)