When creating an image slider, using a cross fade is one of more popular effects. Various sliders use differing techniques to create such an effect. Major techniques I’ve found so far are:
- Method 1: Use an overlay and underlay
<div>
and fade in and out each other’svisibility
. - Method 2: Create a
<div>
matching the exact size of the slider during initialization, play with itsz-index
property, and then fade each other.
Using method-1
, it needs creating two unneeded and over exploited block elements most commonly a div
. First the background property is applied to the underlayed-div then the overlayed-div is faded out and subsequently changed in z-index property to be positioned lower that other one.
Using method-2
, it creates only one extra div and now container’s background is changed and the overlay div is faded out. Implementing this effect now limits the transition effect that can be build (but this is not the question in hand).
The reason why I believe this could be better is because extra elements will always have to be added to give the effect. Due to this reason, creating effects like that of nivo-slider
requires creating 12 to 15+ divs to give the effect.
Adding up to number of elements to create the element does not seem to be the right way.
Are the methods I mentioned above used so much, because there aren’t any other ways? There has to be better ways that to create the cross-fade effect.
So my question is simple,
Is there a better way to create this effect? If yes, What are they?
3
The truth of the matter is, the web wasn’t designed for that. It wasn’t designed for 90% of what it does today. Hacks and web development are closely tied. The best solution is almost always the one that works.
The web is moving towards standardizing the dynamic nature of what’s already out there (HTML5, canvas in your case like thorston mentioned) but it’s not there yet and won’t be for a long time.
Is there a better way to create this effect? Yes, a video editing software would be vastly superior and easier for what you’re trying to do. But you are trying to make a website not a video, albeit with properties analogous to video, so not really. Go with what works, even if it isn’t 100% intuitive.
1