JavaScript
Javascript image rotator viewer
Last Updated (Tuesday, 30 November 1999 00:00) Written by Alex Grande Thursday, 28 August 2008 05:26
JavaScript - JavaScript Design Patterns
See a demo at http://www.alexgrande.com
It is the images on the top right.
I wrote this in Object Oriented format so you can use it again again on page.
Javascript:
var ImageGallery = new Function();
ImageGallery.prototype = {
initialize: function(mainImage, listWrapper){
// large image that is shown
this.mainImage = document.getElementById(mainImage);
// a list of all the anchors for the thumbnails. Must be a tags for graceful degradation
this.thumbnails = document.getElementById(listWrapper).getElementsByTagName("a");
// 0 image is shown already so the first one we want to switch to is the next image or 1
this.i = 1;
// this is a work around to allow calling this within nested functions
var Scope = this;
// Here starts the rotating of the image by first focusing the thumbnail, then switch the primary image
this.start = setInterval(function(){
Scope.focusCall();
// Here we choose 5 seconds in between each image change. You may want to change this.
}, 5000);
// This lets the browser know to do something if one of the thumbnails is clicked
this.clickEvent();
},
// This stops the rotation of the thumbnails. We do that if the user clicks one of them
stop: function(){
if (this.start)
clearInterval(this.start);
},
// When an thumbnail loses focus we must use this css class now
resetBorderColor: function(reset){
reset.getElementsByTagName("img")[0].className="thumbnailDefault";
},
// When the thumbnail gains focus we most give it the corresponding styles
focusBorderColor: function(focused){
focused.getElementsByTagName("img")[0].className="thumbnailFocus";
},
// Here we grab the href of the a tags and make their path be the path of the current image
imageRotator: function(){
this.mainImage.src = this.thumbnails[this.i].href;
this.previousImage = this.thumbnails[this.i];
this.i++;
// This closes the loop for the rotation
if (this.i == this.thumbnails.length)
this.i = 0;
},
// We focus the thumbnail
focusCall: function(){
// reset the last image that was shown
if (typeof this.previousImage != 'undefined')
this.resetBorderColor(this.previousImage);
// Remember the newer one
this.currentImage = this.thumbnails[this.i];
// Give the newer image some focus
this.focusBorderColor(this.currentImage);
var Scope = this;
// Les that have image rotate to the new one 300 miliseconds after the thumbnails get the css focus
window.setTimeout(function(){
Scope.imageRotator()
// You may want to change this number
}, 300);
},
// This is what happens when you click the thumbnails
clickEvent: function(){
var Scope = this;
for (k = 0; k < this.thumbnails.length; k++) {
this.thumbnails[k].onclick = function(){
if (typeof Scope.previousImage != 'undefined')
Scope.resetBorderColor(Scope.previousImage);
Scope.focusBorderColor(this);
// Stop the rotation
Scope.stop();
// This is where the switching happens for the click
Scope.mainImage.src = this.href;
Scope.previousImage = this;
// Make sure to not allow default behavior of the a tag
return false;
}
}
}
}
This is to load it on the window object and create an instance of the viewer
var imageGallery1 = new ImageGallery();
// Not sure where I got this.. I didn't write this but it allows you to load multiple functions on the window.onload.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != "function") {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
var onLoad = function() {
imageGallery1.initialize("index_largepic_display", "index_thumbnail_display");
}
addLoadEvent(onLoad);
For the version on my homepage alexgrande.com here is the CSS and HTML. The CSS is up to you but I suggest following a similar mode with the HTML
HTML:
<div>
div#gallery {
position:relative;
float: left;
overflow: hidden;
width: 65%;
}
img#index_image_display {
border:1px black solid;
margin-bottom:20px;
}
ul#index_thumbnail_display {
list-style-type:none;
position:absolute;
top: 0;
left: 0;
margin-top: 15px;
}
ul#index_thumbnail_display li a {
padding:10px;
}
.thumbnailDefault {border: 1px solid gray !important;}
.thumbnailFocus {border: 1px solid red !important;}
| < Prev | Next > |
|---|
Comments (1)
Test comment
1
Saturday, 30 August 2008 06:01
JoJo
This is just a test comment to make sure we got comments working.


