Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check this : <a href="http://jsfiddle.net/codebombs/ukNmT/" rel="nofollow">http://jsfiddle.net/codebombs/ukNmT/</a></p> <p>I created a simple slide show with fading effect using jQuery.</p> <p><strong>HTML Code</strong></p> <pre><code>&lt;div id='items'&gt; &lt;div class='item first'&gt;Item 1&lt;/div&gt; &lt;div class='item'&gt;Item 2&lt;/div&gt; &lt;div class='item'&gt;Item 3&lt;/div&gt; &lt;div class='item'&gt;Item 4&lt;/div&gt; &lt;div class='item'&gt;Item 5&lt;/div&gt; &lt;/div&gt; &lt;ul id='controls'&gt; &lt;li id='prev'&gt;Prev&lt;/li&gt; &lt;li id='play'&gt;Play&lt;/li&gt; &lt;li id='pause'&gt;Pause&lt;/li&gt; &lt;li id='next'&gt;Next&lt;/li&gt; &lt;/ul&gt; </code></pre> <p><strong>CSS Code</strong></p> <pre><code>#items { position : relative; width : 400px; height : 200px; top : 20px; left : 20px; } .item { position : absolute; background-color : #eee; border : 1px solid #ccc; width : 398px; height : 198px; display :none; text-align : center; font-size : 72px; } .first{ display : block; } #controls { margin-top : 30px; } li { display : inline-block; padding : 5px; border : 1px solid #ccc; background-color : #eee; cursor : pointer; } #play { display : none; } </code></pre> <p><strong>JavaScript Code</strong></p> <pre><code>//To store timeout id var timeoutId; var slideImage = function( step ) { if ( step == undefined ) step = 1; //Clear timeout if any clearTimeout ( timeoutId ); //Get current image's index var indx = $('.item:visible').index('.item'); //If step == 0, we don't need to do any fadein our fadeout if ( step != 0 ) { //Fadeout this item $('.item:visible').fadeOut(); } //Increment for next item indx = indx + step ; //Check bounds for next item if ( indx &gt;= $('.item').length ) { indx = 0; } else if ( indx &lt; 0 ) { indx = $('.item').length - 1; } //If step == 0, we don't need to do any fadein our fadeout if ( step != 0 ) { //Fadein next item $('.item:eq(' + indx + ')').fadeIn(); } //Set Itmeout timeoutId = setTimeout ( slideImage, 5000 ); }; //Start sliding slideImage(0); //When clicked on prev $('#prev').click(function() { //slideImage with step = -1 slideImage ( -1 ); }); //When clicked on next $('#next').click(function() { //slideImage with step = 1 slideImage ( 1 ); }); //When clicked on Pause $('#pause').click(function() { //Clear timeout clearTimeout ( timeoutId ); //Hide Pause and show Play $(this).hide(); $('#play').show(); }); //When clicked on Play $('#play').click(function() { //Start slide image slideImage(0); //Hide Play and show Pause $(this).hide(); $('#pause').show(); }); </code></pre>
 

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