Image Slideshow with Navigation Buttons Using Javascript


A simple JavaScript code snippet from Bluefish...
A simple JavaScript code (Photo credit: Wikipedia)

I have already posted JavaScript codes for simple image slideshows without caption, with caption and Jquery fade effect animation. As some of our visitors asked how to add a next button to the slide, I have posted this JavaScript code for creating image slideshow with Navigation Buttons. It may help more for creating awesome image animations for your website or blog.

Code for Creating Image Slideshow with Navigation Buttons 


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

<script type="text/javascript"> 
var i = 0;
var image = new Array();
// LIST OF IMAGES
image[0] = "image-1.png";
image[1] = "image-2.png";
image[2] = "image-3.png";
var k = image.length-1;
var caption = new Array();

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

function next(){
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; }
}

function prev(){
var el = document.getElementById("mydiv");
el.innerHTML=caption[i];
var img= document.getElementById("slide");
img.src= image[i];
if(i >0 ) { i--;}
else { i = k; }
}

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:none;background-color:transparent;">
<tr>
<td>
<img onclick="prev()" title="Previous" alt ="Prev" height="25" width="15"src="prev.jpg"/>
</td>
<td>
<img name="slide" id="slide" alt ="my images" height="285" width="485" src="image-1.png"/>
</td>
<td>
<img onclick="next()" title="Next" alt ="Next" height="25" width="15" src="next.jpg"/>
</td>
</tr>
<tr>
<td>
</td>
<td align="center"style="font:small-caps bold 15px georgia; color:blue;">
<div id ="mydiv"></div>
</td>
<td>
</td>
</tr>
</table>


You Might also view the following Related Posts

ليست هناك تعليقات:

إرسال تعليق

فيديو الأسبوع