Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to spawn an animated div with creation/destruction conditions
    primarykey
    data
    text
    <p>I am trying to create a series of divs that slowly fade into existence and float a distance across their container. Once a floating div (floater) goes outside the container/off-screen it reappears where it was spawned and fades back in again, looping forever.</p> <p>I have successfully created the basic animation, but I don't know of an efficient way to have several divs spawned and float/follow each other to the edge of the container.</p> <p>Does anyone know how I could do this?</p> <p>Here is what I have so far for my single div animation:</p> <pre><code>function floaters() { $('.floater').each(function(){ $(this).animate({'opacity': '1'}, {queue: false, duration: 5000}) ; $(this).animate({'left': '-=652'}, 2000, 'linear', function(){ $(this).css('left', '622px') ; $(this).css('opacity', '0.0') ; floaters() ; }) ; }) } </code></pre> <p>I am unsure if 'each' is what I should be using or not.</p> <hr> <p>You can see what I have done so far here and what I'm trying to achieve:</p> <p><a href="http://jsfiddle.net/Y73TZ/" rel="nofollow">http://jsfiddle.net/Y73TZ/</a></p> <p>I basically want a series of boxes to follow each other and loop once each one reaches the end of the page, exactly the same as this one box does, but each with a random starting distance between them.</p> <hr> <p>@r0m4n Kind of what I wanted, but I want each box to start at the same location with a delay between them so their is an even amount of space between each box and no more than say 8 boxes present at one time. What would be the adjustments to the script to achieve this?</p> <hr> <p>I have redone the script in the hopes that it will better explain what I'm trying to do. I have used setInterval instead as it gives me better control of the events I want to happen at certain points in the animation:</p> <pre><code>var creationTimer ; var moveTimer ; function floaters() { var firstTime = 1 ; moveTimer = setInterval(function(){ $('.floater').each(function(){ var pos = $(this).position() ; $(this).css('left', (pos.left-2)+'px') ; if(pos.left &lt; -32 || firstTime == 1) { firstTime = 0 ; $(this).css('left', '620px') ; $(this).hide() ; $(this).show(500); } if(pos.left &lt; 500 &amp;&amp; pos.left &gt; 450) { clearTimeout(creationTimer) ; creationTimer = setTimeout(function(){ $('#latest').append('&lt;div class="floater"&gt;&lt;/div&gt;') ; }, 1000) clearInterval(moveTimer); } }) }, 50) } </code></pre> <p>At the point where clearInterval(moveTimer) is called, I want a new div to appear and follow the preceeding box. I want that cycle to repeat X amount of times. I hope I am explaining this well enough. Thanks for your help so far.</p> <hr> <p>Can anyone help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload