Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In JQuery you can use animation-chaining and the <a href="http://api.jquery.com/delay/" rel="nofollow">.delay()</a> function, to create a delay for a specific amount of time between the fadeOut and fadeIn.</p> <p>A simple example, which delays for 1 sec, fades-out a div, delays for 1 sec, and then fades it back in again: </p> <p><strong>Javascript:</strong></p> <pre><code>$('#myDiv').delay(1000).fadeOut(300).delay(1000).fadeIn(300); </code></pre> <p><strong>HTML:</strong></p> <pre><code>&lt;div id="myDiv" style="background-color: red; width:200px; height:200px;"&gt;&lt;/div&gt; </code></pre> <p><strong>Javascript V2:</strong></p> <p>Or a little more advanced, if we want to change the background-color of the div to yellow before fading in again. (This is similar to your case, where you want to change picture before the image element is being faded in again)</p> <pre><code>$('#myDiv').delay(1000).fadeOut(300, function() { $(this).css("background-color","yellow"); // set background to yellow before fading in }).delay(1000).fadeIn(300); </code></pre> <p><strong>Javascript V3:</strong></p> <p>Finally, a way to also make the animation loop is the following: </p> <pre><code>function start() { $('#myDiv').delay(1000) .fadeOut(300, function() {$(this).css("background-color","yellow");}) .delay(1000) .fadeIn(300, start); } start(); </code></pre> <p>I've created a function called start, which is called when the animation stops in order to start it again. (and of course it's also called to start it for the first time) </p> <p>.<br> <strong>Your example:</strong> </p> <p>From the code you posted on jsfiddle, something like this will work: (sorry for the inline javascript)</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ var imgsrc = ["images/c1.png", "images/c2.png", "images/c3.png", "images/c4.png", "images/c5.png"]; var imgnum = 0; function start() { $('#cfifoimg').delay(1000).fadeOut(300, function() { imgnum++; if(imgnum&gt;4) {imgnum=0;} $(this).attr("src",imgsrc[imgnum]); }).delay(1000).fadeIn(300, start); } start(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="cfifo"&gt; &lt;img id="cfifoimg" src="images/c1.png"/&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Important:</strong> Not all JQuery functions can be chained like this - see <a href="http://api.jquery.com/delay/" rel="nofollow">http://api.jquery.com/delay/</a> for more information</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.
    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