Galleriffic and Colorbox - Quick Fix

I wrote a small web gallery application and used the galleriffic template as my base layout. I liked the image preloading and the image swap effect and didn’t want any other template for my gallery script, so I modified all the links to get a “german gallery”.

The only thing that I didn’t like was the " Show Original" link beneath the image on the right side so I decided to use colorbox to spice it up a bit.

First of all you have to add the Colorbox javascript code to your webserver and add a link to the javascript file

1
<script type="text/javascript" src="/js/jquery.colorbox.js"></script>

Normally, if you click on the " Show Original" link you get the image in the top left corner of your browser without any css/html styling and it looks some kind of rough to me for such a great layout, so I added all files of colorbox to my server, added the needed sourcecode and added a “rel=‘cbviewer’” tag to the “Show Original” link and after some minutes of testing I recognized, that this link is finally broken and nothing happend, when clicking on it. Nothing comes up, no nice colorbox popup, no image in an empty browser window, nothing.

The first thing that I checked was the errorconsole of firefox and found an entry of colorbox that indicates some incompatibilities between the galleriffic javascript and colorbox.

I googled a bit and found a lot of fixes for this problem and the more i found they become bigger and more complex so I started to find a fix for this on my own. I wanted a small solution, 5 lines of code, or something to get colorbox working.

I had two ideas. The first one was a small fix to get the colorbox popup if I click the “Show Original” link, without the ‘previous’ and ’next’ links in colorbox. The second idea I had was a link beside the “start slideshow” to start a colorbox slideshow in fullscreen mode with the start position at the current gallery image. I started with the small fix for colorbox and will add a small tutorial for the fullscreen slideshow later.

Here is the first small fix, to get the “Show Original” link to work

The javascript code for the colorbox (You can read more about the JQuery live function here )

1
2
3
4
$(document).ready(function(){
 $("a[rel='cbviewer']").live('click' , function (e) {
   $.colorbox({href:$(this).attr("href"), title:$(this).attr("title")});
});});

Explanation: The JQuery .live() function binds an event handler to the root dom object. The advantage of this technique is, that you can add event handlers to dom objects that doesn’t exist at the time you initialize your event handlers.

This quick fix adds a click event handler to all links ( “a href” elements ) with the rel attribute “cbviewer”.

1
$("a[rel='cbviewer']").live('click' , function (e) {

When a user clicks on the specified link, the colorbox is called and read some information from the link to get all information that is needed for displaying the image correctly

1
2
3
4
$.colorbox({
href:$(this).attr("href"),
title:$(this).attr("title")
});

As you can see, this call reads the href attribute to get the image path and the title attribute for displaying the title information.

Here is the html code you have to edit:

1
<a rel="cbviewer" title="MyTitle" onclick="return false;" href="/path/to/image.jpg">Show Original</a>

Here are 2 pictures with the fix built in into the Galleriffic sourcecode and what it look like in the end.

Galleriffic QuickfixGalleriffic QuickfixGalleriffic Quickfix - what it looks like

Licensed under CC BY-NC-SA 4.0
Zuletzt aktualisiert am Jun 27, 2011 22:04 UTC
comments powered by Disqus
Developer / Inventor / Creator
Erstellt mit Hugo
Theme Stack gestaltet von Jimmy