How To Build An iPhoto Image Flipper Using The jQuery Cycle Plugin
Posted April 7th, 2010 in ProgrammingTags: How To, JavaScript, Programming
My favorite jQuery plugin by far has to be the jQuery Cycle Plugin. It’s a generic slideshow plugin, but it’s versatile enough that I’ve used it to build a slider, a portfolio, and just recently, an iPhoto-like image flipper. This was inspired from the CJ Image FlipBox. Unfortunately, the way the plugin is designed, you can’t mesh it with Fancybox because the plugin creates an anchor and an image which intercept the mouseclicks preventing the Fancybox from activating. So to get around the problem, I used the Cycle plugin to create my own.
<div id="gallery1" class="gallery"> <a rel="gallery1" href="/path/to/fullsize1.jpg"><img src="/path/to/thumbnail1.jpg" width="50" height="50" alt="" /></a> <a rel="gallery1" href="/path/to/fullsize2.jpg"><img src="/path/to/thumbnail2.jpg" width="50" height="50" alt="" /></a> <a rel="gallery1" href="/path/to/fullsize3.jpg"><img src="/path/to/thumbnail3.jpg" width="50" height="50" alt="" /></a> </div>
The HTML is rather straight forward. You’ve got a <div> as a container for a bunch of thumbnails, each linked to a matching fullsize image. The CSS on the gallery has fixed dimensions matching the dimensions of the thumbnails with overflow set to hidden.
$(".gallery").cycle({ speed: 0, timeout: 0 }); $(".gallery").mousemove(function(e){ var n = $(this).children().length; var w = $(this).width(); var x = e.pageX - this.offsetLeft; var i = Math.floor(x/w * n); $(this).cycle(i); }); $(".gallery a").fancybox();
As for the JavaScript, it’s refreshingly short, which just goes to show how much heavylifting jQuery and the two plugins do. Let’s start with the Cycle activation. The speed parameter is set to 0. This means that the transition is instantaneous when an image flips. Setting the timeout parameter to 0 prevents the Cycle plugin from auto-advancing. So far nothing new for anyone who’s used the Cycle plugin.
The next part actually sets up the effect. Just like when you mouseover an Event group in iPhoto, whenever the mouse moves within the gallery div, the position of the mouse determines the index of the image to display. First we count the number of images within the gallery. Next, we get the width of the gallery. Then we get the horizontal position of the mouse within the gallery’s coördinate system. Finally, we use those three values to calculate the index. Using this index, we tell the Cycle plugin which image to display.
Now that the iPhoto effect is working, all that’s left is to activate the Fancybox plugin and we’re done.
Do you have an example?
Thanks
Hi Brian,
thanks for sharing… it helped me a lot. But your code didn’t seem to work for me because of an issue with offsetLeft.
I’ve added these small changes:
javascript.addLine(“ var x = e.pageX — $(this).offset().left;”);
javascript.addLine(“ var i = Math.floor(x / (w / n));”);
Best regards,
Guido
Hi Brian,
thanks for sharing… it helped me a lot. But your code didn’t seem to work for me because of an issue with offsetLeft.
I’ve added these small changes:
var x = e.pageX — $(this).offset().left;
var i = Math.floor(x / (w / n));
Best regards,
Guido