How to generate a simple popup using jQuery的答案呈现以下简短的 jsFiddle http://jsfiddle.net/SRw67/ 。在 jsFiddle 中你会发现:
function deselect(e) {
$('.pop').slideFadeToggle(function() {
e.removeClass('selected');
});
}
其中函数slideFadeToggle定义为:
$.fn.slideFadeToggle = function(easing, callback) {
return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};
在 jQuery 文档中我发现:
easing A string indicating which easing function to use for the transition.
函数slideFadeToggle有2个参数,但在代码中只传递了一个函数。 jQuery 是否会自动识别该参数是一个函数,从而跳过缓动参数并将该函数作为回调传递?或者为什么当明显缺少缓动参数时这会起作用?
此外,为什么作者使用 $.fn.slideFadeToggle =..?
我在这里找到了What does jQuery.fn mean?这是 $.prototype.slideFadeToggle 的快捷方式,但对我来说没有任何意义。为什么这在这里有用?
最佳答案
Does jQuery automatically identify that the parameter is a function and thus skips the easing parameter and passes the function as the callback?
是的,这正是发生的事情。
通过阅读 jQuery 源代码,您可以看到 animate
对于将两个形式参数传递给它并不是非常讲究。在当前版本中,这是间接完成的:这些参数用于 construct an options object by calling jQuery.Speed
,依次 inspects the types of its arguments以确定哪个对应于什么(undefined
也由 bool 检查隐式处理)。
同样的行为也出现在许多其他 jQuery 方法上;例如无处不在的 on
识别四个参数,其中第二个和第三个是可选的。
关于javascript - jQuery 函数中的缓动参数自动识别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517479/