tags:

views:

176

answers:

3

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">
         <div 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.

+1  A: 

Try using the Cycle Plugin:

$('.contentPanel').cycle();
Kobi
can't use a plugin :(
theDawckta
+1  A: 

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; }
Nick Craver
this one isn't working for me in my test project
theDawckta
@theDawckta - You'll have to be a bit more specific, "isn't working" isn't ever helpful. I'm guessing your project markup differs from what you posted, so it probably needs to be adjusted accordingly, if you update with actual code I can show you how to do that.
Nick Craver
NM I didn't see your link to show the working code. That is a cool page. I have it working now. I had the fadeContent in the head. Thank you for the response it was exactly what I was looking for.
theDawckta
A: 

Hi,

Nick, you did a very cool page to show the example. Thank you very much for that.

I have a problem though, I need only one image to fadeIn and out without stopping.

So I took your code and removed what I thought was not needed for my case. The problem is, the animation runs only once and disappears.

My code:

In the head tag I put this:

How do I add the code here????

I tried severl times with the enter code here button, but it doesn't display my code... //

My image has the class banner.

Can you please help me and tell me what am I doing wrong?

Thanks,

Sigal

Sigal