jQuery fadeIn事件? [英] jQuery fadeIn event?
问题描述
当使用jQuery淡入一个元素时,是否可以获得某种事件通知?IE.如果有一个'fadeInEvent',我会尝试类似的
Is it possible to get some kind of event notifciation when an element has faded in using jQuery? I.e. if there was a 'fadeInEvent' I would try something like
$('elements').delegate('selector', 'fadeInEvent', function() {
alert('someId has faded in');
});
我知道 jQuery.fadeIn()有一个回调函数,例如
I know that there is a callback function for jQuery.fadeIn(), e.g.
$('#someId').fadeIn('fast', function() {
alert('callback when someId has faded in');
});
,但我宁愿使用事件解决方案.我还使用:visible
做了一些原型设计,但是在淡入完成之前它会返回 true
.
but I would rather use an event solution if possible. I also did some prototyping using :visible
, but it returns true
before the fade in has completed.
推荐答案
您可以在回调中触发一个自定义事件:
You can trigger a custom event in the callback:
$("#someId").fadeIn("fast", function() {
$(this).trigger("fadeInComplete");
});
该事件将像大多数事件一样使DOM树冒泡,因此您可以使用 on
(jQuery 1.7 +), bind
或委托
:
The event will bubble up the DOM tree like most events, so you can capture it on any of the ancestor elements with on
(jQuery 1.7+), bind
or delegate
:
$("#someAncestor").on("fadeInComplete", function() {
//Element has finished fading in.
});
这篇关于jQuery fadeIn事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!