我知道问题标题令人困惑。这也使我的搜索很难找到解决方案。
假设我有这些代码,当.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);
最佳答案
要将您自己的参数传递给这些回调并确保在这些回调中将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
}
}