Introduction
In my last article I showed you how to make a space saving image scrollbar complete with a pseudo animation effect. In this article we are going to extend the image scrollbar into a full image viewer with scrollbar. We will add functionality to change the large image, image title and image description when you hover over the thumbnail images in the scrollbar. The key topics covered in this project include the two dimensional Array, innerHTML, setAttribute, setTimeout and onmouseover.
<html >
<head>
<title>Scroll Bar Example</title>
</head>
<body onload="javascript:preloadThumbnails()">
<script language="javascript" type="text/javascript">
var imageData= new Array(10)
createTwoDimensionalArray(3);
imageData[0][0]="image1.png";
imageData[1][0]="image2.png";
imageData[2][0]="image3.png";
imageData[3][0]="image4.png";
imageData[4][0]="image5.png";
imageData[5][0]="image6.png";
imageData[6][0]="image7.png";
imageData[7][0]="image8.png";
imageData[8][0]="image9.png";
imageData[9][0]="image10.png";
imageData[0][1]="Grasslands";
imageData[1][1]="Tree Canopy";
imageData[2][1]="In the Clouds";
imageData[3][1]="Sunflower Bud";
imageData[4][1]="Morning Dew";
imageData[5][1]="Hillside Tree";
imageData[6][1]="Winter Wonderland";
imageData[7][1]="Mountain View";
imageData[8][1]="Babbling Brook";
imageData[9][1]="Wooded Trail";
imageData[0][2]="This is the description for the first image. Here will be where we give details on the image that is currently being viewed.";
imageData[1][2]="This is the description for the second image. Here will be where we give details on the image that is currently being viewed.";
imageData[2][2]="This is the description for the third image. Here will be where we give details on the image that is currently being viewed.";
imageData[3][2]="This is the description for the fourth image. Here will be where we give details on the image that is currently being viewed.";
imageData[4][2]="This is the description for the fifth image. Here will be where we give details on the image that is currently being viewed.";
imageData[5][2]="This is the description for the sixth image. Here will be where we give details on the image that is currently being viewed.";
imageData[6][2]="This is the description for the seventh image. Here will be where we give details on the image that is currently being viewed.";
imageData[7][2]="This is the description for the eighth image. Here will be where we give details on the image that is currently being viewed.";
imageData[8][2]="This is the description for the ninth image. Here will be where we give details on the image that is currently being viewed.";
imageData[9][2]="This is the description for the tenth image. Here will be where we give details on the image that is currently being viewed.";
var imageIndexFirst = 0;
var imageIndexLast = 3;
var continueScroll = 0;
var maxIndex = 9;
var minIndex = 0;
function createTwoDimensionalArray(arraySize) {
for (i = 0; i < imageData.length; ++ i)
imageData[i] = new Array(arraySize);
}
function preloadThumbnails() {
imageObject = new Image();
for (i = 0; i < imageData.length; ++ i)
imageObject.src = imageData[i][0];
}
function changeCellText(cellId,myCellData){
document.getElementById(cellId).innerHTML = myCellData;
}
function changeImage(ImageToChange,MyimageData){
document.getElementById(ImageToChange).setAttribute('src',MyimageData)
}
function changeImageAlt(ImageToChange,imageData){
document.getElementById(ImageToChange).setAttribute('alt',imageData)
}
function changeImageTitle(ImageToChange,imageData){
document.getElementById(ImageToChange).setAttribute('title',imageData)
}
function changeImageOnMouseOver(ImageToChange,imageIndex){
document.getElementById(ImageToChange).setAttribute('onmouseover','handleThumbOnMouseOver(' + imageIndex + ');')
}
function handleThumbOnMouseOver(imageIndex){
changeImage('imageLarge',imageData[imageIndex][0]);
changeCellText('imageTitleCell',imageData[imageIndex][1]);
changeCellText('imageDescriptionCell',imageData[imageIndex][2]);
changeImageAlt('imageLarge',imageData[imageIndex][1] + ' - ' + imageData[imageIndex][2]);
changeImageTitle('imageLarge',imageData[imageIndex][1] + ' - ' + imageData[imageIndex][2]);
}
function scrollImages(scrollDirection) {
var currentIndex;
if (scrollDirection == 'up')
{
if (imageIndexLast != maxIndex)
{
document.getElementById('scrollPreviousCell').setAttribute('style','color: Black')
document.getElementById('scrollNextCell').setAttribute('style','color: Black')
imageIndexLast = imageIndexLast + 1;
imageIndexFirst = imageIndexFirst + 1;
if (imageIndexLast == maxIndex)
{
document.getElementById('scrollNextCell').setAttribute('style','color: Silver')
}
currentIndex = imageIndexLast;
changeImage('scrollThumb4',imageData[currentIndex][0]);
changeImageOnMouseOver('scrollThumb4',currentIndex);
currentIndex = imageIndexLast - 1;
setTimeout("changeImage('scrollThumb3',imageData[" + currentIndex + "][0])",25);
setTimeout("changeImageOnMouseOver('scrollThumb3'," + currentIndex + ")",25);
currentIndex = imageIndexLast - 2;
setTimeout("changeImage('scrollThumb2',imageData[" + currentIndex + "][0])",50);
setTimeout("changeImageOnMouseOver('scrollThumb2'," + currentIndex + ")",50);
currentIndex = imageIndexLast - 3;
setTimeout("changeImage('scrollThumb1',imageData[" + currentIndex + "][0])",75);
setTimeout("changeImageOnMouseOver('scrollThumb1'," + currentIndex + ")",75);
setTimeout("scrollAgain('" + scrollDirection + "')",1000);
}
}
else
{
if (imageIndexFirst != minIndex)
{
document.getElementById('scrollPreviousCell').setAttribute('style','color: Black')
document.getElementById('scrollNextCell').setAttribute('style','color: Black')
imageIndexLast = imageIndexLast - 1;
imageIndexFirst = imageIndexFirst - 1;
if (imageIndexFirst == minIndex)
{
document.getElementById('scrollPreviousCell').setAttribute('style','color: Silver')
}
currentIndex = imageIndexFirst;
changeImage('scrollThumb1',imageData[currentIndex][0]);
changeImageOnMouseOver('scrollThumb1',currentIndex);
currentIndex = imageIndexFirst + 1;
setTimeout("changeImage('scrollThumb2',imageData[" + currentIndex + "][0])",25);
setTimeout("changeImageOnMouseOver('scrollThumb2'," + currentIndex + ")",25);
currentIndex = imageIndexFirst + 2;
setTimeout("changeImage('scrollThumb3',imageData[" + currentIndex + "][0])",50);
setTimeout("changeImageOnMouseOver('scrollThumb3'," + currentIndex + ")",50);
currentIndex = imageIndexFirst + 3;
setTimeout("changeImage('scrollThumb4',imageData[" + currentIndex + "][0])",75);
setTimeout("changeImageOnMouseOver('scrollThumb4'," + currentIndex + ")",75);
setTimeout("scrollAgain('" + scrollDirection + "')",1000);
}
}
}
function scrollAgain(scrollDirection)
{
if (continueScroll == 1)
{
scrollImages(scrollDirection);
}
}
function scrollPrevious() {
continueScroll = 1;
scrollImages('down');
}
function scrollNext() {
continueScroll = 1;
scrollImages('up');
}
function scrollStop() {
continueScroll = 0;
}
</script>
<table border="0" cellpadding="5" cellspacing="0" width="700px">
<tr>
<td align="center" colspan="6" style="font-weight: bold; font-size: 18pt; color: silver;
background-color: maroon" id="imageTitleCell">
Grasslands</td>
</tr>
<tr>
<td align="center" colspan="6" style="background-color: maroon">
<img height="400" src="image1.png" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid;
border-bottom: 1px solid" width="400" id="imageLarge" alt="default" /></td>
</tr>
<tr>
<td align="left" colspan="6" style="padding-right: 100px; padding-left: 100px; color: white;
background-color: maroon" id="imageDescriptionCell">
This is the description for the first image. Here will be where we give details
on the image that is currently being viewed.</td>
</tr>
<tr>
<td id="scrollPreviousCell" style="color: Silver" onmouseover="scrollPrevious();" onmouseout="scrollStop();">
<< Previous</td>
<td>
<img id="scrollThumb1" height="100" src="image1.png" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid;
border-bottom: 1px solid" width="100" onmouseover="handleThumbOnMouseOver(0);" /></td>
<td>
<img id="scrollThumb2" height="100" src="image2.png" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid;
border-bottom: 1px solid" width="100" onmouseover="handleThumbOnMouseOver(1);" /></td>
<td>
<img id="scrollThumb3" height="100" src="image3.png" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid;
border-bottom: 1px solid" width="100" onmouseover="handleThumbOnMouseOver(2);" /></td>
<td>
<img id="scrollThumb4" height="100" src="image4.png" style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid;
border-bottom: 1px solid" width="100" onmouseover="handleThumbOnMouseOver(3);" /></td>
<td id="scrollNextCell" style="color: Black" onmouseover="scrollNext();" onmouseout="scrollStop();">
Next >></td>
</tr>
</table>
</body>
</html>