Resources:
Version one of the image gallery is with the JavaScript embedding in the head element and it plays automatically. There is no navigation links and no CSS.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Image Gallery - Beginner</title>
</head>
<body>
</body>
</html>
<body>
<div>
<img src="images/flowers/african_violets - Large.jpg" name="slideshow" alt=""/>
</div>
</body>
<title>Image Gallery - Beginner</title>NOTE: This is an array that have references to TEN flower images in the images/flowers folder.
<script>
var imageArray = new Array (
"images/flowers/african_violets - Large.jpg",
"images/flowers/Bird_Of_Paradise - Large.jpg",
"images/flowers/daffodils - Large.jpg",
"images/flowers/daisy flowers - Large.jpg",
"images/flowers/iris flowers - Large.jpg",
"images/flowers/lily flowers - Large.jpg",
"images/flowers/Lotus Flower - Large.jpg",
"images/flowers/rose-flower - Large.jpg",
"images/flowers/sun_flowers - Large.jpg",
"images/flowers/tulip flowers - Large.jpg"
);
</script>
alert(imageArray);
</script>
// alert(imageArray);
console.log(imageArray);
</script>
// alert(imageArray);NOTE: Variables are usually placed at the top of the code.
// console.log(imageArray);
var imageIndex = 0;
var numberOfImages = imageArray.length-1;
// Time delay between slides (milliseconds)
var delay = 3000;
</script>
// Main function ------------NOTE: The updateImage() function is used to check if an image exist in the document, index the imageIndex variable by 1, and then update the src attribute of the img element with an id of slideshow. The timer is used to run the updateImage() function every three seconds.
function updateImage() {
if (document.images) {
imageIndex++;
document.slideshow.src = imageArray[imageIndex];
}
}
//Timer --------------
setInterval("updateImage()", delay);
</script>
// Main function ------------NOTE: This "if" statement checks to see if the imageIndex variable is greater than the total number of images and if so set the imageIndex to zero to start the slideshow over again.
function updateImage() {
if (document.images) {
imageIndex++;
if (imageIndex > numberOfImages) {
imageIndex = 0;
}
document.slideshow.src = imageArray[imageIndex];
}
}
//Timer --------------
setInterval("updateImage()", delay);
</script>
Version two of the image gallery is similar to the beginner version except navigation links (next, play/pause/ and previous) have been added with the necessary JavaScript code.
View ImageGallery_intermediate
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Image Gallery - Intermediate</title>
</head>
<body>
</body>
</html>
<body>NOTE: Notice that the three links have an onclick event handler that are assigned to three functions.
<div>
<img src="images/flowers/african_violets - Large.jpg" name="slideshow" alt=""/>
</div>
<div>
<a href="#" onclick="updateImage(-1)">Previous</a> |
<a href="#" onclick="playSlides()">Play/Pause</a> |
<a href="#" onclick="updateImage(1)">Next</a>
</div>
</body>
<title>Image Gallery - Intermedia</title>NOTE: This is an array that have references to TEN flower images in the images/flowers folder.
<script>
var imageArray = new Array (
"images/flowers/african_violets - Large.jpg",
"images/flowers/Bird_Of_Paradise - Large.jpg",
"images/flowers/daffodils - Large.jpg",
"images/flowers/daisy flowers - Large.jpg",
"images/flowers/iris flowers - Large.jpg",
"images/flowers/lily flowers - Large.jpg",
"images/flowers/Lotus Flower - Large.jpg",
"images/flowers/rose-flower - Large.jpg",
"images/flowers/sun_flowers - Large.jpg",
"images/flowers/tulip flowers - Large.jpg"
);
</script>
// Variables -----------------------------------NOTE: Variables are usually placed at the top of the code.
var imageIndex = 0;
var numberOfImages = imageArray.length - 1;
var direction = 0;
var pause = false;
var play;
// Time delay between slides (milliseconds)
var delay = 3000;
</script>
// Update Image Function -------------------------NOTE: The updateImage() function is used to see if an image exist in the document, index the imageIndex variable by 1 and then update the src property of the img element with an id of slideshow. The timer is used to run the updateImage() function every three seconds.
function updateImage(direction) {
if (document.images) {
imageIndex = imageIndex + direction;
if (imageIndex > numberOfImages) {
imageIndex = 0;
}
if (imageIndex < 0) {
imageIndex = numberOfImages;
}
document.slideshow.src = imageArray[imageIndex];
}
}
// playSlides Function ---------------------------
function playSlides() {
if (pause == true) {
pause = false;
window.clearInterval(play);
}
else if (pause == false) {
pause = true;
play = setInterval("updateImage(1)", delay);
}
}
</script>
There are two versions of the advanced Image Gallery. One with and array of objects and the other with two arrays. This one uses an Array of Objects.
View ImageGallery_advanced (With An Array of Objects)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Image Gallery - Advanced Using An Array of Objects</title>
</head>
<body>
</body>
</html>
<body>NOTE: Notice that the three lines in the script element uses the getElementById method to ascertain the name of the three buttons to change their value.
<div id="gallery_container">
<img id="slideImage" src=" " alt=" " name="slideshow" width="580" height="360">
<p id = "captionName">...</p>
<p id = "pageStatus">...</p>
<button id="playPauseButton" href="#" onClick="playSlides()">Play</button>
<a id="previousPhotoButton" href="#" onClick="updateImage(-1)"><img src="previous_button.png" width="40" height="40" alt=""/></a>
<a id="nextPhotoButton" href="#" onClick="updateImage(1)"><img src="next_button.png" width="40" height="40" alt=""/></a>
</div>
<script>
// Set up first slide based on array of objects ----------------------------------
document.getElementById("slideImage").src = imageArray[0].FlowerImageName;
document.getElementById("captionName").innerHTML = imageArray[0].FlowerCaption;
document.getElementById("pageStatus").innerHTML = "Photo 1 of " + imageArray.length;
</script>>
</body>
<title>Image Gallery - Advanced Using An Arry of Objects</title> <style>
body{
font-family:Arial, Helvetica, sans-serif;
font-weight:bold;
}
#gallery_container{
margin: 0 auto;
width:580px;
height:360px;
text-align: center;
border: 8px double gray;
}
#previousPhotoButton {
width: 40px;
height: 40px;
position: relative;
left: -250px;
top: -290px;
}
#nextPhotoButton{
width: 40px;
height: 40px;
position: relative;
left: 200px;
top: -290px;
}
#playPauseButton{
text-align:center;
margin: 0 auto;
position: relative;
left:40px
} </style>
</style>NOTE: This is an ARRAY OF OBJECTS.
<script>
// Create an array of objects with properties =========================================================
var imageArray = [
{FlowerImageName:"images/flowers/african_violets - Large.jpg", FlowerCaption:"African Violets text goes here..."},
{FlowerImageName:"images/flowers/Bird_Of_Paradise - Large.jpg", FlowerCaption:"Bird Of Paradise text goes here..."},
{FlowerImageName:"images/flowers/daffodils - Large.jpg", FlowerCaption:"Daffodils text goes here..."},
{FlowerImageName:"images/flowers/daisy flowers - Large.jpg", FlowerCaption:"Daisy flowers text goes here..."},
{FlowerImageName:"images/flowers/iris flowers - Large.jpg", FlowerCaption:"Iris flowers text goes here..."},
{FlowerImageName:"images/flowers/lily flowers - Large.jpg", FlowerCaption:"Lily flowers text goes here..."},
{FlowerImageName:"images/flowers/Lotus Flower - Large.jpg", FlowerCaption:"Lotus flower text goes here..."},
{FlowerImageName:"images/flowers/rose-flower - Large.jpg", FlowerCaption:"Rose flower text goes here..."},
{FlowerImageName:"images/flowers/sun_flowers - Large.jpg", FlowerCaption:"Sun flowers text goes here..."},
{FlowerImageName:"images/flowers/tulip flowers - Large.jpg", FlowerCaption:"Tulip flowers text goes here..."}
];
</script>
// Time delay between slides (milliseconds)NOTE: Variables are usually set at the top of the code.
var delay = 3000;
// Update image -------------------------------------------------
var imageIndex = 0;
var numberOfImages = imageArray.length - 1;
var firstImageName = imageArray[9].FlowerImageName;
</script>
// update Image Function --------------------------------------NOTE: The updateImage() function is used to see if an image exist in the document, index the imageIndex variable by 1 and then update the src property of the img element with an id of slideshow. The timer is used to run the updateImage() function every three seconds.
function updateImage(direction) {
if (document.images) {
imageIndex = imageIndex + direction;
if (imageIndex > numberOfImages) {
imageIndex = 0;
}
if (imageIndex < 0) {
imageIndex = numberOfImages;
}
document.slideImage.src = imageArray[imageIndex].FlowerImageName;
document.getElementById("captionName").innerHTML = imageArray[imageIndex].FlowerCaption;
document.getElementById("pageStatus").innerHTML = "Photo " + (imageIndex+1) + " of " + imageArray.length;
}
}
// Play / Pause Slides ----------------------------------------
var play;
var pause = false; // This is called a flag
// Time delay between slides (milliseconds)
var delay = 3000;
function playSlides() {
// You could have used "!pause" instead of "pause == false"
if (pause == false)
{
pause = true;
play = setInterval("updateImage(1)", delay);
document.getElementById("playPauseButton").innerHTML = "Pause";
}
else if (pause == true)
{
// You could have simple used "pause" instead of "pause == true"
pause = false;
window.clearInterval(play);
document.getElementById("playPauseButton").innerHTML = "Play";
}
}
</script>
There are two versions of the advanced Image Gallery. One with and array of objects and the other with two arrays. This one uses two arrays.
View ImageGallery_advanced (With Two Arrays)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Image Gallery - Advanced Using Two Arrays</title>
</head>
<body>
</body>
</html>
<body>NOTE: Notice that the three lines in the script element uses the getElementById method to ascertain the name of the three buttons to change their value.
<div id="gallery_container">
<img id="slideImage" src=" " alt=" " name="slideshow" width="580" height="360">
<p id = "captionName">...</p>
<p id= "pageStatus">...</p>
<button id="playPauseButton" href="#" onClick="playSlides()">Play</button>
<a id="previousPhotoButton" href="#" onClick="updateImage(-1)"><img src="previous_button.png" width="40" height="40" alt=""/></a>
<a id="nextPhotoButton" href="#" onClick="updateImage(1)"><img src="next_button.png" width="40" height="40" alt=""/></a>
</div>
<script>
// Set up first slide based on array ----------------------------------
document.getElementById("slideImage").src = imageArray[0];
document.getElementById("captionName").innerHTML = captionArray[0];
document.getElementById("pageStatus").innerHTML = "Photo 1 of " + imageArray.length;
</script>
</body>
<title>Image Gallery - Advanced Using Two Arrays</title> <style>
body{
font-family:Arial, Helvetica, sans-serif;
font-weight:bold;
}
#gallery_container{
margin: 0 auto;
width:580px;
height:360px;
text-align: center;
border: 8px double gray;
}
#previousPhotoButton {
width: 40px;
height: 40px;
position: relative;
left: -250px;
top: -290px;
}
#nextPhotoButton{
width: 40px;
height: 40px;
position: relative;
left: 200px;
top: -290px;
}
#playPauseButton{
text-align:center;
margin: 0 auto;
position: relative;
left:40px
} </style>
</style>NAME: This version uses TWO ARRAYS instead of a SINGLE ARRAY OF OBJECTS.
<script>
// Create an array for images --------------------------
imageArray = new Array (
"images/flowers/african_violets - Large.jpg",
"images/flowers/Bird_Of_Paradise - Large.jpg",
"images/flowers/daffodils - Large.jpg",
"images/flowers/daisy flowers - Large.jpg",
"images/flowers/iris flowers - Large.jpg",
"images/flowers/lily flowers - Large.jpg",
"images/flowers/Lotus Flower - Large.jpg",
"images/flowers/rose-flower - Large.jpg",
"images/flowers/sun_flowers - Large.jpg",
"images/flowers/tulip flowers - Large.jpg"
);
// Create an array for captions --------------------------
captionArray = new Array (
"African Violets text goes here...",
"Bird Of Paradise text goes here...",
"Daffodils text goes here...",
"Daisy flowers text goes here...",
"Iris flowers text goes here...",
"Lily flowers text goes here...",
"Lotus flower text goes here...",
"Rose flower text goes here...",
"Sun flowers text goes here...",
"Tulip flowers text goes here..."
);
</script>
// Variables -----------------------------NOTE: Variables are usually set at the top of the code.
var imageIndex = 0;
var numberOfImages = imageArray.length - 1;
var direction = 0;
var play;
var pause = false; // This is called a flag
// Time delay between slides (milliseconds)
var delay = 3000;
</script>
// Update Image Function -------------------NOTE: The updateImage() function is used to see if an image exist in the document, index the imageIndex variable by 1 and then update the src property of the img element with an id of slideshow. The timer is used to run the updateImage() function every three seconds.
function updateImage(direction) {
if (document.images) {
imageIndex = imageIndex + direction;
if (imageIndex > numberOfImages) {
imageIndex = 0;
}
if (imageIndex < 0) {
imageIndex = numberOfImages;
}
document.slideImage.src = imageArray[imageIndex];
document.getElementById("captionName").innerHTML = captionArray[imageIndex];
document.getElementById("pageStatus").innerHTML = "Photo " + (imageIndex+1) + " of " + imageArray.length;
}
}
// Play / Pause Slides ---------------------------
function playSlides() {
// You could have used "!pause" instead of "pause == false"
if (pause == false)
{
pause = true;
play = setInterval("updateImage(1)", delay);
document.getElementById("playPauseButton").innerHTML = "Pause";
}
else if (pause == true)
// You could have simple used "pause" instead of "pause == true"
{
pause = false;
window.clearInterval(play);
document.getElementById("playPauseButton").innerHTML = "Play";
}
}
</script>
This expert version uses third-party CSS files and JavaScript to create an interactive slide show.
Image Gallery Expert
<title>Slick jQuery Sideshow: Demo</title>
<link rel="stylesheet" href="slideshow.css">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="final.js" type="text/javascript"></script>
</head>