[英]Pass jQuery to .animate Callback
I've setup a simple animator method like so: 我已经设置了一个简单的动画师方法,如下所示:
Animator: function (obj, aniArgs, duration, aniEasArgs, completeFunction) {
obj.stop(true, true).animate(aniArgs, {
duration: duration,
queue: false,
specialEasing: aniEasArgs,
complete: function () {
if (completeFunction !== null) {
};
}
});
return obj;
},
What I'd like to do is be able to pass in string of jQuery to run in the complete callback. 我想做的是能够传递jQuery字符串以在完整的回调中运行。 Everything else works fine. 其他一切都很好。 How, for example, would I pass in 例如,我将如何传入
$('#someElement').hide(500);
into the callback? 进入回调?
Thanks! 谢谢!
First, create a function with the code you want to run: 首先,使用您要运行的代码创建一个函数:
function complete() {
$('#someElement').hide(500);
}
Then, pass that function in when you call Animator: 然后,在调用Animator时传递该函数:
Animator( obj, aniArgs, duration, aniEasArgs, complete );
Or, you can put the function inline: 或者,您可以将函数内联:
Animator( obj, aniArgs, duration, aniEasArgs, function() {
$('#someElement').hide(500);
});
Those both work the same way, you can use either style depending on what makes your code easier to read. 这些都以相同的方式工作,您可以使用任何一种样式,具体取决于使代码更易于阅读的内容。
Either way, change your Animator code to: 无论哪种方式,将您的Animator代码更改为:
if( completeFunction ) {
completeFunction();
};
Or, you'll often see that written this way: 或者,你会经常看到这样写的:
completeFunction && completeFunction();
They both do exactly the same thing. 他们都做了完全相同的事情。 It's just a matter of taste which you use, just thought I'd mention both variations so you'll recognize them when you see them. 这只是你使用的味道问题,我想我会提到两种变化,所以当你看到它们时你会认出它们。
Note that the !== null
test is not required here, and in fact isn't what you want. 请注意,此处不需要!== null
测试,实际上并不是您想要的。 That's because if the caller does not pass in a completeFunction
, the value of that variable will be undefined
, not null
, so your test won't work. 那是因为如果调用者没有传入一个completeFunction
,那么该变量的值将是undefined
,而不是null
,因此您的测试将无效。 Interestingly enough, you can use != null
in this case, because that will test for both null
and undefined
and treat them the same. 有趣的是,在这种情况下你可以使用!= null
,因为它将测试null
和undefined
并对它们进行相同的处理。 But really you just don't need this explicit test at all, if the only thing you're checking for is whether the caller provided a completeFunction
callback or not. 但实际上你根本不需要这个显式测试,如果你唯一要检查的是调用者是否提供了一个completeFunction
函数回调。
Try 尝试
Animator: function (obj, aniArgs, duration, aniEasArgs, completeFunction) {
obj.stop(true, true).animate(aniArgs, {
duration: duration,
queue: false,
specialEasing: aniEasArgs,
complete: function () {
if (jQuery.isFunction(completeFunction )) {
completeFunction.apply(this, arguments);
};
}
});
return obj;
},
Theb Theb
x.Animator(obj, aniArgs, duration, aniEasArgs, function(){
$('#someElement').hide(500);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.