问题标题令人困惑,我知道。它也使我的搜索难以找到解决方案。
假设我有这些代码,当.animate()选项是外部对象时,如何将参数传递给complete
或step
?
var someObj = {
onComplete: function(e) {
console.log(e); // return undefined
},
onStep: function(e) {
console.log(e); // return undefined too
},
}
var optionObj = {
duration: 300,
easing: 'linear',
complete: someObj.onComplete(e), // this is not working
step: someObj.onStep(e) // this is not working
}
$('div').animate({width: "500px", height: "500px"}, optionObj);
答案 0 :(得分:3)
要将您自己的参数传递给这些回调并确保this
在这些回调中正确设置为someObj
,您可能需要额外的闭包:
var optionObj = {
duration: 300,
easing: 'linear',
complete: function() {
someObj.onComplete(e); // assumes 'e' is defined somewhere
},
step: function(now, fx) { // jQuery automatically supplies these parameters
someObj.onStep(e); // so you need to pass your own instead
}
}
答案 1 :(得分:2)
尝试定义您的对象,如下所示。从complete: someObj.onComplete
移除了(e),因为使用(e)
将调用该函数并将返回值分配给optionObj.complete
和optionObj.step
undefined
。< / p>
var optionObj = {
duration: 300,
easing: 'linear',
complete: someObj.onComplete,
step: someObj.onStep
}