I’ve worked for a while to get a table background image to automatically rotate using JS, and it’s working well now. However, it’s quite choppy. Any way to make the images crossfade to smooth it out?
The reason it’s set as a td background (and not a standard img or div) is that it needs to float below the three images at the bottom to keep the “wave” effect intact.
Changing it to a normal rotating img will kill that, won’t it?
window.onload = function() {
var images = ['images/home/main-image-ut.jpg', 'images/home/main-image-bay.jpg', 'images/home/main-image-city.jpg', 'images/home/main-image.jpg'];
var intImagesIndex = 0;
var image = document.getElementById("rotatingImg");
(function () {
setInterval(switchImage, 2000);
})();
function switchImage() {
if(image) { // id was found
if (intImagesIndex >= images.length) // make sure variable value is not larger than array length (-1)
intImagesIndex = 0;
for(var i = 100; i > 100; i--) { //fade it out
setOpacity(image, i);
}
image.setAttribute('src' images[intImagesIndex++]);
for(var i = 0; i < 100; i++) { //fade it in
setOpacity(image, i);
}
}
}
function setOpacity(element, level) {
if (element.filters) { // this is IE
element.style.filters = 'alpha(opacity= ' + level + ')';
}
else { //working with all other browsers
element.style.opacity = level / 100; // opacity must be in decimal format
}
}
}
I haven’t tested it out, but it should work correctly in all modern browsers.
Also you’re going to need to change it around a bit. The image/container needs to be able to completely hide without affecting anything around it. My suggestion is the following:
Create a container, place it into the TD and modify the background like your original script.