简体   繁体   English

如何淡入和淡出多个元素?

[英]How can I fade in and fade out multiple elements?

I can get it to work with just one, but if I add multiple it fades them in and out at the same time (instead of one after another).我可以让它只使用一个,但如果我添加多个它会同时淡入和淡出它们(而不是一个接一个)。 Here's what I have tried:这是我尝试过的:

$(document).ready(function(){
setTimeout(function(){
$("#head1").fadeToggle(1000);
}, 1200);
$("#head1").fadeToggle(1000);
setTimeout(function(){
$("#head2").fadeToggle(1000);
}, 1200);
$("#head2").fadeToggle(1000);
});

This fades them in and out at the same time.这同时淡入和淡出它们。 How can successive fading be achieved using jQuery?如何使用 jQuery 实现连续衰落?

You have to add up to the timeout duration like:您必须添加超时持续时间,例如:

 $(document).ready(function(){
    
        var duration = 1200;
        setTimeout(function(){
        $("#head1").fadeToggle(1000);
        }, duration);
        $("#head1").fadeToggle(1000);//this will execute right away you will need another timeout
        setTimeout(function(){
        $("#head2").fadeToggle(1000);
        }, duration * 2);
        $("#head2").fadeToggle(1000);//this will execute right away you will need another timeout
   });

I ended up coming across jQuery's.delay().我最终遇到了 jQuery's.delay()。 Here's what ended up working best:以下是最终效果最好的:

$("#head1").delay(1000).fadeIn(1000).delay(500).fadeOut(1000);  
$("#head2").delay(3600).fadeIn(1000).delay(500).fadeOut(1000);
$("#head3").delay(6200).fadeIn(1000);

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

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