简体   繁体   English

如何淡出一组 HTML 元素,然后用 jQuery 淡入其中一个?

[英]How to fade out a set of HTML elements and then 1 of them fades in with jQuery?

Seems simple enough.看起来很简单。 I want an all <div> s to fadeOut() and then 1 of the <div> s to fadeIn() .我想要一个所有<div> s到fadeOut() ,然后1个<div> s到fadeIn() But I get crazy behavior with something like this ( jsfiddle with 3 <div> s )但是我用这样的东西( jsfiddle with 3 <div> s )得到了疯狂的行为

<a href="#" onclick="$('div').fadeOut(2000, function() {$('div#first').fadeIn();}); return false;">Show first div</a>
<br/>
<a href="#" onclick="$('div').fadeOut(2000, function() {$('div#second').fadeIn();}); return false;">Show second div</a>
<br/>
<a href="#" onclick="$('div').fadeOut(2000, function() {$('div#third').fadeIn();}); return false;">Show third div</a>
<br/>
<div style="display:none" id="first">First div</div>
<div style="display:none" id="second">Second div</div>
<div style="display:none" id="third">Third div</div>

Sometimes the old div and the new div will disappear.有时旧的 div 和新的 div 会消失。 Sometimes the new div will appear, fade and then reappear.有时新的 div 会出现,淡出然后重新出现。

I think what's happening is when all div s fadeOut , each one of them calls the fadeIn function and that gets a little screwy.我认为发生的事情是,当所有divfadeOut每个人都调用了fadeIn函数,这变得有点棘手。 So what's a solution here to fadeIn only after all have been faded out?那么这里有一个解决方案,以fadeIn毕竟已经淡出?

The problem is your callback gets called 3 times, one for each matched div after the animation for that div completes.问题是您的回调被调用 3 次,在该 div 的动画完成后,每个匹配的 div 调用一次。

Here is an excerpt from the jQuery documentation :以下是jQuery 文档的摘录:

Callback Function回调函数

If supplied, the callback is fired once the animation is complete.如果提供,则在动画完成后触发回调。 This can be useful for stringing different animations together in sequence.这对于将不同的动画按顺序串在一起很有用。 The callback is not sent any arguments, but this is set to the DOM element being animated.回调不发送任何参数,但它被设置为正在设置动画的 DOM 元素。 If multiple elements are animated, it is important to note that the callback is executed once per matched element, not once for the animation as a whole.如果对多个元素进行动画处理,请务必注意,回调对每个匹配的元素执行一次,而不是对整个动画执行一次。

As of jQuery 1.6, the .promise() method can be used in conjunction with the deferred.done() method to execute a single callback for the animation as a whole when all matching elements have completed their animations ( See the example for .promise() ).从 jQuery 1.6 开始,.promise() 方法可以与 deferred.done() 方法结合使用,当所有匹配的元素都完成它们的动画时,为整个动画执行单个回调(请参阅 .promise 的示例() )。

here is a solution to your problem:这是您问题的解决方案:

<a href="#" onclick="$('div').fadeOut(2000);$('div').promise().done(function() {$('div#first').fadeIn();}); return false;">Show first div</a>
<br/>
<a href="#" onclick="$('div').fadeOut(2000);$('div').promise().done(function() {$('div#second').fadeIn();}); return false;">Show second div</a>
<br/>
<a href="#" onclick="$('div').fadeOut(2000);$('div').promise().done(function() {$('div#third').fadeIn();}); return false;">Show third div</a>
<br/>
<div style="display:none" id="first">First div</div>
<div style="display:none" id="second">Second div</div>
<div style="display:none" id="third">Third div</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM