JQuery fade with loop and delay

前端 未结 7 840
谎友^
谎友^ 2020-12-10 20:37

I have 2 Divs stacked on top of each other.

I need a really simple function that will:

a) Wait for 3 seconds and then b) FadeOut the top Div to reveal the se

相关标签:
7条回答
  • 2020-12-10 21:06

    Here's what you're looking for (I think). It uses an unordered-list, but you could switch it out for div's or just put your div's inside of the list items like I've done below.

    Here's the jQuery...

    $(document).ready(function() {
    
         var j = 0;
         var delay = 2000; //millisecond delay between cycles
         function cycleThru(){
                 var jmax = $("ul#cyclelist li").length -1;
                 $("ul#cyclelist li:eq(" + j + ")")
                         .animate({"opacity" : "1"} ,400)
                         .animate({"opacity" : "1"}, delay)
                         .animate({"opacity" : "0"}, 400, function(){
                                 (j == jmax) ? j=0 : j++;
                                 cycleThru();
                         });
                 };
    
         cycleThru();
    
    });
    

    ...and some starting css...

    ul#cyclelist {width:200px;border:solid;position:relative;overflow:hidden;height:200px}
    ul#cyclelist li {font-size:1.4em;padding:20px;opacity:0;position:absolute}
    

    You already have your HTML, but in case you need an example...

    <ul id="cyclelist">
      <li><div>First Div</div></li>
      <li><div>Second Div</div></li>
      <li><div>Third Div</div></li>
    </ul>
    

    I'd love to take credit for this, but it's straight from CSS-Tricks http://css-tricks.com/snippets/jquery/cycle-through-a-list/

    0 讨论(0)
提交回复
热议问题