javascript - 类中的jquery动画回调函数
问题描述
我想要一个元素的连续动画。我创建了一个类并使用了 aj 查询动画方法,如下所示
class street {
constructor(streetLength){
this.lineLength=0;
this.space=0;
this.speed=4;
this.streetLength=streetLength;
}
moveLine(element){
$(element).animate({top:'+=1px'},100,function(){
console.log('animating');
this.moveLine(element);
});
}
}
当我在回调中再次使用 moveLine(element) 函数继续动画时,我收到一条错误消息,指出“moveline 不是函数。
解决方案
没有测试它,但“这个”上下文可能有问题,试试这个:
class street {
constructor(streetLength){
this.lineLength=0;
this.space=0;
this.speed=4;
this.streetLength=streetLength;
}
moveLine(element){
$this = this;
$(element).animate({top:'+=1px'},100,function(){
console.log('animating');
$this.moveLine(element);
});
}
}
推荐阅读
- php - 如何在我授予 php API 权限的 facebook 页面和组上发布
- typescript -
没有在打字稿中解决 - python - 导入 qsharp ModuleNotFoundError:没有名为“qsharp”的模块
- pandas - Python:使用 sum 特殊列分组并保留初始行
- javascript - (Javascript递归)为什么“价值”在没有增量代码的情况下增加?
- wordpress - 如何正确地将图像控件添加到 Wordpress 定制器?
- python - 数据框仅划分特定列后显示所有列
- json - 公共属性数组设置值
- algorithm - 求 a、b 和 c 的值,使得 X^a * Y^b * Z^c 最接近 N 并且 a+b+c 是最小的
- csv - Spark 在读取 CSV 时跳过不良记录