Ajax Light Box V2

Overview

Lightbox is a simple, unobtrusive script used to overlay images on the current page. It’s a snap to setup and works on all modern browsers.

What’s New in Version 2

  • Image Sets: group related images and navigate through them with ease
  • Visual Effects: fancy pants transitions
  • Backwards Compatibility: yes!

How to Use

Part 1 – Setup

  1. Lightbox 2 uses the Prototype Framework and Scriptaculous Effects Library. You will need to include these three Javascript files in your header (in this order).
    <script type="text/javascript" src="js/prototype.js"></script>
    <script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
    <script type="text/javascript" src="js/lightbox.js"></script>
    
  2. Include the Lightbox CSS file (or append your active stylesheet with the Lightbox styles).
    <link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
    
  3. Check the CSS and make sure the referenced prev.gif and next.gif files are in the right location. Also, make sure the loading.gif and close.gif files as referenced near the top of the lightbox.js file are in the right location.

Part 2 – Activate

  1. Add a rel="lightbox" attribute to any link tag to activate the lightbox. For example:
    <a href="images/image-1.jpg" rel="lightbox" title="my caption">image #1</a>
    

    Optional: Use the title attribute if you want to show a caption.

  2. If you have a set of related images that you would like to group, follow step one but additionally include a group name between square brackets in the rel attribute. For example:
    <a href="images/image-1.jpg" rel="lightbox[roadtrip]">image #1</a>
    <a href="images/image-2.jpg" rel="lightbox[roadtrip]">image #2</a>
    <a href="images/image-3.jpg" rel="lightbox[roadtrip]">image #3</a>
    

    No limits to the number of image sets per page or how many images are allowed in each set. Go nuts!

Download

Changelog

  • v2.04 – 3/9/08
    • NEW – Upgraded Prototype from v1.4 to v1.6.0.2
    • NEW – Moved label text into configuration for easier localization
    • UPDATED – Code cleaned up. Respect for the global namespace and native javascript objects.
    • FIXED – Caption displays as “null” when viewing an uncaptioned image after viewing a captioned image.
    • FIXED – Clicking ‘close’ button shifts layout as link focus’ dotted line appears.
  • v2.03.3 – 5/21/07 – Support for horizontally scrolling pages. Added updateImageList method for ajax’y pages.
  • v2.03.2 – 4/30/07 – Fixed animated gif support in IE/Opera.
  • v2.03.1 – 4/18/07 – Fixed embed hiding. Overlay opacity var added to config. Image sets w/Imagemaps fix. Clearfix removed.
  • v2.03 – 4/10/07 – Improved keyboard navigation. Animation off toggle. Hides Flash movies under overlay. Imagemap support. Valid CSS.
  • v2.02 – Fixed layout issues caused by multiline captions. Added keyboard navigation.
  • v2.01 – Centering in IE6 (any DOCTYPE) fixed. Smoothed out resize transition.

Support

It doesn’t work at all. The image opens up in a new page. What’s wrong?
This is commonly caused by a conflict between scripts. Check your body tag and look for an onload attribute. Example:
<body onload="MM_preloadImages(‘/images/menu_on.gif’)…;">
A quick fix to this problem is to append the initLightbox() to the onload attribute as so:
<body onload="MM_preloadImages(‘/images/menu_on.gif’)…;initLightbox()">
It doesn’t work if I click an image link before the page has finished loading.
The script is activated only after the page has finished loading.
The shadow overlay doesn’t stretch to cover full browser window.
Remove the default margin and padding from the body tag. Add body{ margin: 0; padding: 0; } to your stylesheet.
Can I insert links in the caption?
Yes, but you need to convert quotes and greater and less than symbols into their html entity equivalents. For example:
<a href="images/image-4.jpg" rel="lightbox" title="&lt;a href=&quot;link.html&quot;&gt;my link&lt;/a&gt;">Image</a>
Can I display flash, video, or other content using the script?
Sorry, photos only. For other content, google for Lightbox modifications or try an alternative script such as Cody Lindley’s ThickBox.
Can the script be called from an iframe?
If you’re using iframes, try the Lytebox modification which has better support.
Can I use the script in a commercial project?
Yes. It is licensed under the Creative Commons Attribution 2.5 License. A donation would be nice, hint hint.

As for proper attribution, all that is required is that you leave my name and link atop the lightbox javascript file. I do appreciate an html link, but it is not required.

Leave a Comment.