How To Create Simple Image Slideshow Using JavaScript ?

We can create attractive and eye catching website by using image slideshows, flash and other designs. There are so many pre-build JavaScript files and jquery plugins can be found on the web, but today i am going to tail about Creating simple and easy to understand code for image slideshow using JavaScript.

 

Code for creating simple image slideshow

Just copy and paste the code below where you want to place slideshow and change the location of the images.

<script language="JavaScript">
var i = 0;
var path = new Array();
 
// LIST OF IMAGES
path[0] = "image_1.gif";
path[1] = "image_2.gif";
path[2] = "image_3.gif";

function swapImage()
{
   document.slide.src = path[i];
   if(i < path.length - 1) i++; else i = 0;
   setTimeout("swapImage()",3000);
}
window.onload=swapImage;
</script>
<img height="200" name="slide" src="image_1.gif" width="400" />

Demo slideshow for the above code

var i = 0;
var path = new Array();

// LIST OF IMAGES
path[0] = “http://4.bp.blogspot.com/-dNgc-Kv8Wmw/UdhW7hddvII/AAAAAAAAAL8/rLduN41JUR4/s1600/image_1.gif”;
path[1] = “http://1.bp.blogspot.com/–ZDsNEez8jU/UdhWz3bNOYI/AAAAAAAAALs/8vaqtfBiUD4/s1600/image_2.gif”;
path[2] = “http://3.bp.blogspot.com/-041MSyFCvLM/UdhW0L8g2eI/AAAAAAAAAL0/xu4QXtDO53s/s1600/image_3.gif”;

function swapImage_0()
{
document.slide.src = path[i];
if(i < path.length – 1) i++; else i = 0;
setTimeout(“swapImage_0()”,3000);
}

Code for creating slideshow with caption

Just copy and paste the code below where you want to place slideshow and change the location  and caption of the images.
<script type="text/javascript"> 
var i = 0; 
var image = new Array();   
// LIST OF IMAGES 
image[0] = "image_1.gif"; 
image[1] = "image_2.gif"; 
image[2] = "image_3.gif";    
var k = image.length-1;    

var caption = new Array(); 

// LIST OF CAPTİONS  
caption[0] = "Caption for the first image"; 
caption[1] = "Caption for the second image"; 
caption[2] = "Caption for the third image";   

function swapImage(){ 
var el = document.getElementById("mydiv"); 
el.innerHTML=caption[i]; 
var img= document.getElementById("slide"); 
img.src= image[i];  
if(i < k ) { i++;}  
else  { i = 0; } 
setTimeout("swapImage()",5000);  
} 

function addLoadEvent(func) { 
var oldonload = window.onload; 
if (typeof window.onload != 'function') { 
window.onload = func; 
} 
else { 
window.onload = function() { 
if (oldonload) { 
oldonload(); 
} 
func();}}}  
addLoadEvent(function() { 
swapImage(); });  
</script> 

<table style="border:3px solid #00aaff;background-color:#00aaaa;"> 
<tr> 
<td> 
<img name="slide" id="slide" alt ="my images" height="285" width="485" src="image_1.gif"/> 
</td> 
</tr> 
<tr> 
<td align="center"style="font:small-caps bold 15px georgia; color:blue;"> 
<div id ="mydiv"></div> 
</tr> 
</td> 
</table>

 

Demo slideshow for the above code

 

var i = 0;

var image = new Array();

// LIST OF IMAGES

image[0] = “http://4.bp.blogspot.com/-dNgc-Kv8Wmw/UdhW7hddvII/AAAAAAAAAL8/rLduN41JUR4/s1600/image_1.gif”;

image[1] = “http://1.bp.blogspot.com/–ZDsNEez8jU/UdhWz3bNOYI/AAAAAAAAALs/8vaqtfBiUD4/s1600/image_2.gif”;

image[2] = “http://3.bp.blogspot.com/-041MSyFCvLM/UdhW0L8g2eI/AAAAAAAAAL0/xu4QXtDO53s/s1600/image_3.gif”;

var k = image.length-1;

var caption = new Array();

// LIST OF CAPTİONS

caption[0] = “Caption for the first image”;

caption[1] = “Caption for the second image”;

caption[2] = “Caption for the third image”;

function swapImage(){

var el = document.getElementById(“mydiv”);

el.innerHTML=caption[i];

var img= document.getElementById(“slide”);

img.src= image[i];

if(i < k ) { i++;}

else { i = 0; }

setTimeout(“swapImage()”,5000);
}

 

my images
If you wanted to make other JavaScript or CSS image slideshow with different effects, may visit the following posts.

About shuseel

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 5 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.