简体   繁体   中英

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?

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(). 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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