I just threw this together.
Can you keep the little comment at the top of imageRotator.js please.
The object source:
CODE
/*
* Javascript Image Rotator
* @author : Ed Cradock
* @date : 7 January 2008
*
* For all your webdesign needs: visit http://www.zymic.com
*/
var _this;
function imageRotator(viewport, cycleOffset)
{
_this = this;
this.imageStack = new Array;
this.offset = cycleOffset;
var exists = document.getElementById(viewport);
if(!eval(exists))
{
alert('Element ID does not exist.');
}
else
{
this.viewport = viewport;
}
}
imageRotator.prototype.getRandom = function()
{
var random = Math.floor(Math.random() * this.imageStack.length);
return this.imageStack[random];
}
imageRotator.prototype.setViewport = function()
{
document.getElementById(_this.viewport).src = _this.getRandom();
}
imageRotator.prototype.start = function()
{
this.setViewport();
setInterval(function() { _this.setViewport() }, _this.offset);
}
imageRotator.prototype.add = function(imageName)
{
this.imageStack.push(imageName);
}
Example usage:
CODE
<html>
<head>
<script type="text/javascript" src="lib/imageRotator.js"></script>
<script type="text/javascript">
<!--
window.onload = function()
{
rotator = new imageRotator('placeholder', 1000);
rotator.add('images/etch.jpg');
rotator.add('images/lenny.png');
rotator.add('images/snake.jpg');
rotator.start();
}
-->
</script>
</head>
<body>
<img id="placeholder" />
</body>
</html>
CODE
rotator = new imageRotator('placeholder', 1000);
First argument (in this case is 'placeholder') is the element's
id, second is the amount of milliseconds between rotation, which is 1 second in this case.
There's a zip and tarball attached containing a working demo.