Image Cross Fade Transition
Posted on 11th April 2008 — A frequent query and request I receive (and have myself) as a developer is: “how can I fade one image in to another?”.








Posted on 11th April 2008 — A frequent query and request I receive (and have myself) as a developer is: “how can I fade one image in to another?”.
In particular, Nathan Wrigley of pictureandword.com, needed a method one image into another mouse rolls over, and the slowly fade back once the mouse has left.
Image rollovers were the staple JavaScript nugget of the 90s, and for a lot of JavaScript developers I know, one of the starting places their passion for JavaScript. Today, rollovers are a no-brainer – either in CSS or with the simplest of JavaScript:
Today’s challenge is the rollover transition!
Watch the complete screencast (alternative flash version)
(QuickTime version is approx. 20Mb, flash version is streaming)
How to Approach the Problem
There are a few different ways which problem can be solved (and I’d love to hear alternative methods via the comments).
Here are the ways I’m going to go through:
The key to all of these techniques is how the rendered markup (i.e. what the browser finally sees) is arranged: all of which are very similar.
Essentially, the end image for the transition must sit
absolutely in the same position as the starting image.It’s also worth keeping in mind that the images we fade between should be the same size (height & width-wise).
Note: all three of these techniques have a caveat: styling the start or end image may cause the effect to break. I would recommend wrapping the image in a
divorspanand styling that element, as it will require less changes to the JavaScript. Either way: it is always best to test in the target browsers.Two Image Technique
I should start by crediting Karl Swedberg who runs Learning jQuery. He solved Nathan’s transition problem using the following technique.
Karl’s method starts with the two images in the markup: both the start and end images. They are contained in a
divand the end image is contained in a furtherdivwithabsolutepositioning.It is important to note that this technique works best for absolutely position images. Changing the
div.fadetoposition: relativemeans thedivelement remains as ablockelement, anddivwill stretch the with of it’s container element (defaulting to 100%).View the working example and the source
HTML
CSS
Obviously if I had more than one fading image, I would use an ID or alternative class to position the
topandleftCSS properties.jQuery
Single Image Technique
This takes the two image technique further. I like the idea that we should let the JavaScript add the sugar to the markup – in that we should really only want an image tag, and using some method to know what image we want to fade to.
This technique allows us to insert the image in the markup as we would if there were no transition effect, and the image can be inline, rather being positioned absolutely.
We are going to use the
background-imageCSS property to specify the target image to fade to.View the working example and the source
HTML
CSS
Other than the inline background image – none is required. You can also apply the
background-imageusing classes if you like.If we wanted to absolutely position the image, or
float: rightfor instance, the best way to do this (if we want to keep the transition), would be to wrap it in adivand style that element.jQuery
Using jQuery, we execute the following tasks:
spanbackground-imageof our start imagePure CSS Technique
If I’m honest, this final technique is a bit cheeky – but still valid. It uses CSS animations only (currently) available in Safari 3 (and WebKit).
However, this is a great example of how to the leverage the CSS using an iPhone (over using JavaScript).
The HTML is the same rendered HTML from the single image technique – but it requires zero JavaScript.
View the working example and the source (to see the effect, view using Safari 3).
HTML
CSS
Although this is only supported in Safari 3, the roll over still works in Firefox (and could work in IE7 – though not IE6 because
:hoveronly works on anchors) – because it’s changing the image’s opacity on:hover.Taking it Further
I’ve taken the single image technique further in to a complete plugin. It’s designed to allows us to pass options to control the type of bind, delays, callbacks and tests before running the animation.
Download the full plugin
You can see the plugin in action in this simple memory game I put together quickly.
It pulls the latest photos from flickr, shuffles them, and then sets your memory skills to work.
It’s obviously just a quick prototype – and I’m not sure what happens when you go beyond level 5! Enjoy.
You should follow me on Twitter here I tweet about jQuery amongst the usual tweet-splurges!