开发者

jQuery fadein fadeout divs over set interval

I want to fadeOut the first div in a collection and then fadeIn the next div. The fade in out would trigger at a set time. The number of items in the collection is 1 to n. Here is an example of the html;

<div class="contentPanel">
      <div class="content">
         <di开发者_Go百科v style="border: solid 2px black; text-align: center">
            This is first content
         </div>
      </div>
      <div class="content">
         <div style="border: solid 2px black; text-align: center">
            This is second content
         </div>
      </div>
      <div class="content">
         <div style="border: solid 2px black; text-align: center">
            This is third content
         </div>
      </div>
   </div>

So on page load the first "content" class would be visible, after x amount of time, the current "content" would fadeout and the next "content" would fade in. When it got to the nth "content" it would start over, fadeout the nth "content" and fadein the first "content". This behavior would loop continuously.


You could do something pretty compact like this:

function fadeContent() {
  $(".content:hidden​​​​​​​​​​​​​​​​​​​​​​​:first").fadeIn(500).delay(2000).fadeOut(500, function​​​​​() {
      $(this).appendTo($(this).parent()); //stick current at the end
      fadeContent(); //loop
  });
}
fadeContent(); //kick it off the first time

You can see a working example with your exact markup here. This fades the first element in over 500ms, leaves it for 2000ms, fades it out over 500ms, puts the element at the end of the list, and fades in the first in the list, rinse, repeat. The only addition to this you need is CSS to initially hide them all, like this:

.contentPanel .content { display: none; }


Try using the Cycle Plugin:

$('.contentPanel').cycle();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜