A JavaScript overlay image gallery

Update: 31/05/2010

I’m no longer using this technique on the site since the redesign – I’m using jQuery Tools Overlay. The examples below will still work but they use the new overlay library.

We interrupt the travelogues to talk about some geeky stuff that’s been going on around these parts.

I’ve recently been using grids of thumbnails to link to Flickr photos as they relate to blog posts (e.g. my previous post). One thing I’ve wanted to do is find a way to show these images without sending viewers over to Flickr and away from my site for fear that they might get lost (if they are navigationally challenged) or simply be pulled into something more interesting and not return. This would be a good excuse to add a little Script.aculo.us and AJAX sweetness to my blog posts.

What I needed was:

  • To be able to embed images into blog posts and just link to my Flickr account rather than hosting the photos myself;
  • A list of thumbnails for when there are several images to show;
  • To avoid encouraging users to go off-site without resorting to pop-ups and such;
  • A JavaScript solution, ideally, but I wanted to avoid adding more libraries (e.g. Mootools) and if possible re-use the ones my already rather bloated Wordpress theme already uses (in this case Prototype).

Solution:

Here’s an example of the end product. Click on the thumbnails…

Plaza de las Tres Culturas
Plaza de las Tres Culturas
Monument at Plaza de las Tres Culturas
Monument at Plaza de las Tres Culturas
Statue of JPII
Statue of JPII
Basilica de Guadalupe
Basilica de Guadalupe
Obsidian carving
Obsidian carving
Temple of the Moon at Teotihuacán
Temple of the Moon at Teotihuacán
At the Temple of the Sun at Teotihuacán
At the Temple of the Sun at Teotihuacán
 

You can hit escape, click the close button or anywhere outside the box to close the overlay. Mouse over the image and click on previous or next to view other images (when part of a series). Even use keyboard shortcuts (P and N). NICE.

For this I had to use my many and varied skills (such as searching on Google, copying and pasting code snippets, checking my Facebook) to come up with the best solution. The end result was shamefully easy and involved:

  • Floating the thumbnails and captions in a grid (using a DL element) was a nice reusable way of displaying the thumbnails regardless of container and text size. TJK Design has a good scalable technique for this.
  • Modal windows are becoming a good solution for contextual user interactions when used appropriately. They are becoming fairly familiar to users, especially those who use Windows Vista (where they have the annoying tendency to come up every 10 seconds or so), and are quite appropriate for viewing larger images in the context of a gallery.
  • Using AJAX to load the images (as I wanted to load an external image rather than load all the images within the page or launch another window).

I found a few great contenders but I settled on the very excellent Lightbox 2 by Lokesh Dhakar, as it did exactly what I needed, looked good and used prototype and script.aculo.us. If I wanted overlays in general for other interactions or content, I would have gone with one of the scripts previously mentioned, or the very cool ModalBox, and if I was using the Mootools JavaScript framework (which would have been ideal, I really like Mootools) I would have used MOOdalBox.

I was pleased to find I didn’t need to really hack any JavaScript out at all in order to get this going, I just had to make a few changes here and there to tie it in with my current Wordpress scheme.

This type of interaction is something you will see more often in sites rich in functionality. I’m currently starting to use these type of techniques at work in order to build user friendly standards-based e-commerce user interfaces. The down side of the ease of using these types of libraries is that it’s very possible there will be a flurry of overuse leading to massive user annoyance. Use wisely…

5 comments

  1. Nice work and customisation. Yeah, I’ve seen this effect on other sites and have been suitably impressed. It’s really how things should work (besides floating holograms, that is).

  2. Great post! Just the other day funnily enough my boss. Steve forwarded a link to me on how to create a lightbox purely driven by css! A breakthrough for css and for browsers that aren’t Javascript enabled! I’m not sure if a lightbox built using purely css would work in this instance with Flickr but if you’re interested in having a look at the article you can visit it here. You may even already know this?! :)

  3. Anton: Thanks! And I’ll be keeping an eye out for floating holograms. Actually maybe they’re not too far away?

    Eleonor: Thanks for sharing that interesting link, quite useful but as most of the commenters point out, it does use JavaScript (well a DOM event at least) and won’t work without JavaScript enabled. But the advantage of that one is that it uses only one line of JS and doesn’t need a whole JS framework library. But the framework libraries add the sexy extra animation effects, etc. A couple of those guys that commented did come up with some CSS only versions though, which are quite impressive although lack in extra functionality (like navigating the gallery, displaying captions, etc)…

    Don’t be afraid of DOM/JavaScript! Just make sure it works acceptably with JS switched off! Switch off JS and try my example. Works completely accessibly…

  4. Awwww yeah, welcome back to nerd-post-land son! None of this traveling-the-world nonsense – time to get back to the basement!

  5. Thanks, good to be back, all the fresh air was killing me!