SlideShow = {
slideDelay: 5, // Number of seconds to wait before showing a new slide.
fadeLength: 1, // Number of seconds for fading in or out.
hertz: 60 // Frequency of updating the fade effect per second.
};
SlideShow.imageSources = [
'http://preview.impactdesigns-ad.com/HBP/img/basecamp009.jpg',
'http://preview.impactdesigns-ad.com/HBP/img/biohazard007.jpg',
'http://preview.impactdesigns-ad.com/HBP/img/Header.png'
];
SlideShow.showSlide = function (newImage, oldImage) {
var g = SlideShow,
opacityIncrement = 1 / g.hertz,
fade = { interval: {}, count: {} };
function fadeOut() {
oldImage.style.opacity = fade.count.out * opacityIncrement;
if (--fade.count.out == 0) {
window.clearInterval(fade.interval.out);
oldImage.style.visibility = 'hidden';
}
}
function fadeIn() {
newImage.style.opacity = 1 - fade.count.in * opacityIncrement;
if (--fade.count.in == 0) {
window.clearInterval(fade.interval.in);
}
}
function newSlide() {
newImage.style.opacity = 0;
newImage.style.visibility = 'visible';
fade.count.in = g.hertz;
fade.interval.in = window.setInterval(fadeIn, g.fadeLength * 1000 / g.hertz);
}
if (oldImage) {
window.setTimeout(newSlide, g.fadeLength * 1000);
fade.count.out = g.hertz;
fade.interval.out = window.setInterval(fadeOut, g.fadeLength * 1000 / g.hertz);
} else {
newSlide();
}
}
SlideShow.replaceSlide = function () {
var g = SlideShow,
images = g.images,
oldImage = images[g.slideIndex];
g.slideIndex = ++g.slideIndex % images.length;
var newImage = images[g.slideIndex];
g.showSlide(newImage, oldImage);
};
SlideShow.stop = function () {
var g = SlideShow,
interval = g.interval;
window.clearInterval(interval);
}
SlideShow.start = function () {
var g = SlideShow,
imageSources = g.imageSources,
images = g.images = [],
container = document.getElementById('slideContainer'),
background = g.background = document.createElement('div');
background.className = 'background';
for (var i = 0; i < imageSources.length; ++i) {
var image = document.createElement('img');
image.src = imageSources[i];
image.style.visibility = 'hidden';
images.push(image);
background.appendChild(image);
}
container.appendChild(background);
var startInterval = window.setInterval(function () {
for (var i = 0; i < images.length; ++i) {
if (images[i].height == 0) {
return;
}
}
window.clearInterval(startInterval);
g.slideIndex = 0;
g.showSlide(images[g.slideIndex]);
g.interval = window.setInterval(g.replaceSlide, g.slideDelay * 1000);
}, 100);
};
SlideShow.start();
#slideContainer {
font-family: sans-serif;
font-weight: bold;
font-style: italic;
font-size: 48px;
color: #cb4949;
width: 1000px;
height: 700px;
overflow: hidden;
position: relative;
}
#slideContainer span {
padding: 20px;
text-align: center;
position: absolute;
z-index: 10;
}
#slideContainer .background, #slideContainer .background img {
position: absolute;
left: 0;
top: 0;
}
<div id="slideContainer"> <span>This is some text over the slide.</span> </div>
<button onclick="SlideShow.stop()"> stop slide show </button>