问题标题令人困惑,我知道。这也让我很难找到解决方案。
假设我有这些代码,当 .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
}
}
关于javascript - 如何将参数传递给 jQuery。动画选项对象的完整属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12521174/