Image slider with background color transitions and fade effects using JavaScript

I’m working on an image carousel that cycles through different photos while changing the container’s background color. The basic functionality works fine but I’m struggling to add smooth fade transitions between images.

Here’s my current working code:

var photos = ["assets/slide1.png", "assets/slide2.png", "assets/slide3.png"];
var descriptions = [];
var colors = ["#333333", "#0A0F1A", "#444444"];
var activeSlide = 0;
var totalSlides = 3;

function rotateSlides() {
    if (activeSlide == totalSlides) {
        activeSlide = 0;
    }
    var container = document.getElementById('slider_wrapper');
    container.style.backgroundColor = colors[activeSlide];
    var slideImage = document.getElementById('mainSlider');
    slideImage.src = photos[activeSlide];
    slideImage.alt = descriptions[activeSlide];
    activeSlide++;
}
setInterval(rotateSlides, 6000);
<div id="slider_wrapper">
    <img class="slider" src="assets/slide1.png" id="mainSlider"> 
</div>

The rotation and background changes work perfectly but when I try to add fade effects, either the whole slider stops working or nothing happens visually. I attempted using jQuery’s fadeOut method but can’t get the timing right.

Any suggestions on how to implement smooth fade transitions without breaking the existing rotation logic? I’m open to rewriting parts of the code if needed.

Look, manual coding is fine but you’re rebuilding something that could be automated way better.

I’ve built dozens of image sliders and those fade timing issues are everywhere. You get stuck with setTimeout callbacks, preloading logic, and CSS transitions that break when requirements change.

Automation workflows handle this stuff better. Configure smooth fades, background colors, timing, and dynamic loading without writing JavaScript.

The workflow watches your images, handles preloading, and manages DOM updates with proper timing. No more fighting opacity transitions or src swap issues.

When your client wants lazy loading or different effects per image, you update the config instead of rewriting code.

I switched after debugging too many fade timing bugs. Now my sliders work and I focus on design instead of wrestling with transitions.

This is a timing issue. You’re switching images with no transition period, so the fade can’t happen. I’ve hit this same problem before - here’s what works: use a two-step approach with setTimeout. First, fade out your current image by setting opacity to 0. Wait for that transition to finish, then swap the source and fade back in. You’ll need CSS transition on your image element plus JavaScript timeouts to sync the opacity changes with updating the src. This stops the jarring instant switch but keeps your rotation logic intact. The trick is changing the source during fade-out, not at the same time.

skip jquery. use css transitions 4 fades. set img opacity to 0, change src, then fade back to 1. it’s smoother and keeps your code intact. just add transition: opacity 0.5s ease-in-out in your img css.

wrap your img in a div with overflow: hidden, then slide new images in from the side while the old ones slide out. way less buggy than opacity tricks and you don’t need two image elements or preloading headaches.

Had this exact problem last month. Don’t mess with your current setup - just preload the next image in a hidden container and cross-fade between them. Make a second img element with position absolute and opacity set to zero. When you transition, fade out the visible image while fading in the hidden one that’s already loaded with the new source. After it’s done, swap their roles for next time. This kills that annoying blank flicker you get when changing src mid-fade since the browser isn’t scrambling to load anything. Way better than trying to sync opacity changes with src updates.