Image Slideshow (Photo credit: Wikipedia) |
If you want to place links on the images or captions of the image slideshow using JavaScript codes, you can do it by creating a new array of links along with the array of images and captions and adding some codes on 'swap' function to rotate links with the image and caption rotation in the slideshow. Here I have written codes for creating JavaScript image slideshow with links, you can use these codes on your website or blog to create attractive image slideshow.
Code for Creating Slideshow with Links on Image
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";
var link= new Array();
// LIST OF LINKS
link[0] = "http://www.siteforinfotech.com/";
link[1] = "http://www.siteforinfotech.com/p/tutorial.html";
link[2] = "http://www.siteforinfotech.com/p/mcqs.html";
function swapImage(){
var el = document.getElementById("mydiv");
el.innerHTML=caption[i];
var img = document.getElementById("slide");
img.src= image[i];
var a = document.getElementById("link");
a.href= link[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>
<a name="link" id="link" href="http://www.siteforinfotech.com/" target="_blank"><img name="slide" id="slide" alt
="my images" height="285" width="485" src="image-1.png"/></a>
</td>
</tr>
<tr>
<td align="center"style="font:small-caps bold 15px georgia; color:blue;">
<div id ="mydiv"></div>
</td>
</tr>
</table>
Code for Creating Slideshow with Links on Caption
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";
var link= new Array();
// LIST OF LINKS
link[0] = "http://www.siteforinfotech.com/";
link[1] = "http://www.siteforinfotech.com/p/tutorial.html";
link[2] = "http://www.siteforinfotech.com/p/mcqs.html";
function swapImage(){
var el = document.getElementById("mydiv");
el.innerHTML=caption[i];
var img = document.getElementById("slide");
img.src= image[i];
var a = document.getElementById("link");
a.href= link[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 name="slide" id="slide" alt ="my images" height="285" width="485" src="image-1.png"/>
</td>
</tr>
<tr>
<td align="center"style="font:small-caps bold 15px georgia; color:blue;">
<a name="link" id="link" href="http://www.siteforinfotech.com/" target="_blank"><div id
="mydiv"></div></a>
</td>
</tr>
</table>
You Might also view the following Related Posts
0 التعليقات: