Shadowbox was designed with both the web hobbyist and the experienced programmer in mind. As such, it can easily be added to any web page with very little time and effort using the default settings. At the same time, Shadowbox offers a great deal of flexibility for those users who need to fine-tune the details of its appearance and operation for a given project.
This page contains detailed instructions on how to set up and run Shadowbox from both perspectives.
After you download a copy of the code with all of the options you need, decompress the archive (unzip it) and upload the entire folder to your web server. The exact location is not important since Shadowbox will automatically detect where it is installed when it runs. However, it is important that you keep all of the files together. After you have uploaded the code to your server, simply link to the JavaScript and CSS files as described below.
The simplest way to set up Shadowbox is to include the JavaScript and CSS files in the <head> of your document (web page) and then call Shadowbox.init, like this:
<link rel="stylesheet" type="text/css" href="shadowbox.css">
<script type="text/javascript" src="shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init();
</script>
The following example is a bit more complex and uses an object literal to specify some options. Options passed to this method will become the default for all instances of Shadowbox on the page.
<link rel="stylesheet" type="text/css" href="shadowbox.css">
<script type="text/javascript" src="shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init({
handleOversize: "drag",
modal: true
});
</script>
Next you need to tell Shadowbox which links you want it to open. The simplest way to do this is through your HTML markup. If you're going to take this route, at the very least you must add a rel="shadowbox" attribute to each link. For example, say you have this link to an image on your page:
<a href="myimage.jpg">My Image</a>
In order to set up this link for use with Shadowbox, simply change it to this:
<a href="myimage.jpg" rel="shadowbox">My Image</a>
That's it! Clicking on this link should now open up the image in Shadowbox.
NOTE:The word lightbox will also work here. This feature was added for compatibility with the originalLightbox script. Also note that because HTML area tags do not support the rel attribute, you cannot use this method to set them up for use with Shadowbox. Instead, use Shadowbox.setup as describedbelow.
If you would like to display a title for your image, simply add a title attribute to the link.
<a href="myimage.jpg" rel="shadowbox" title="My Image">My Image</a>
You must explicitly tell Shadowbox the dimensions to use to display content other than images. This is done by adding a few parameters to the end of the rel attribute, separated by semi-colons. To specify a movie's height and width (in pixels), use the height and width parameters.
<a href="mymovie.swf" rel="shadowbox;height=140;width=120">My Movie</a>
In addition to displaying single images and movies, Shadowbox is also capable of displaying galleries of content. In order to designate a link as part of a gallery, you must add the gallery name to the rel attribute between square brackets immediately following the word shadowbox. The following markup creates a gallery called "Vacation" with two pictures.
<a href="beach.jpg" rel="shadowbox[Vacation]">The Beach</a>
<a href="pier.jpg" rel="shadowbox[Vacation]">The Pier</a>
Galleries may be composed of content of many different types. The following markup demonstrates how various media can be combined into a single gallery.
<a rel="shadowbox[Mixed];" href="myimage.jpg">jpg</a>
<a rel="shadowbox[Mixed];width=520;height=390" href="myswf.swf">swf</a>
<a rel="shadowbox[Mixed];width=292;height=218" href="mymovie.mp4">movie</a>
<a rel="shadowbox[Mixed]" href="mywebsite.html">iframe</a>
If you don't want to add to your markup, you don't have to. Shadowbox may be manipulated and controlled with only JavaScript. This use is slightly more complex, but it has several benefits including the fact that your HTML markup will be cleaner and you can more easily integrate Shadowbox into an existing project.
If you were paying attention in the section about markup, you'll notice that there are several properties that are commonly present on a Shadowbox content object. They are listed in the table below.
Property |
Description |
content |
The actual content of the object (e.g. URL, HTML code, etc.) |
player |
The abbreviated name of the player to use for the object (optional, can be automatically determined in most cases) |
title |
The title to use for the object (optional) |
gallery |
The name of the gallery the object belongs to (optional) |
height |
The height of the object (in pixels, only required for movies and Flash) |
width |
The width of the object (in pixels, only required for movies and Flash) |
options |
A set of options to use for this object |
When using markup, each of these properties is automatically derived from the link element itself using theShadowbox.buildObject method. But now that you know what's really going on behind the scenes, you can just pass objects (or arrays of objects) that contain these properties to Shadowbox.open as in the following example:
<link rel="stylesheet" type="text/css" href="shadowbox.css">
<script type="text/javascript" src="shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init({
// let's skip the automatic setup because we don't have any
// properly configured link elements on the page
skipSetup: true
});
window.onload = function() {
// open a welcome message as soon as the window loads
Shadowbox.open({
content: '<div id="welcome-msg">Welcome to my website!</div>',
player: "html",
title: "Welcome",
height: 350,
width: 350
});
};
</script>
You can also use Shadowbox.setup to manually set up link elements for use with Shadowbox. This can be useful, for example, if you have a page that is updated via Ajax with links being created and destroyed dynamically.
<link rel="stylesheet" type="text/css" href="shadowbox.css">
<script type="text/javascript" src="shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init({
// skip the automatic setup again, we do this later manually
skipSetup: true
});
window.onload = function() {
// set up all anchor elements with a "movie" class to work with Shadowbox
Shadowbox.setup("a.movie", {
gallery: "My Movies",
autoplayMovies: true
});
};
</script>
When using this method to set up links with Shadowbox, you may mix in the link parameters with the options argument as the second parameter. However, the same parameters will apply to all links set up in the same call, so you may want to make separate calls to Shadowbox.setup for each link.
Options
The table below contains a list of options that may be used to configure Shadowbox.
Option |
Description |
animate |
Set this false to disable all fancy animations (except fades). This can improve the overall effect on computers with poor performance. Defaults to true. |
animateFade |
Set this false to disable all fading animations. Defaults to true. |
animSequence |
The animation sequence to use when resizing Shadowbox. May be either "wh" (width first, then height), "hw" (height first, then width), or "sync" (both simultaneously). Defaults to"sync". |
autoplayMovies |
Set this false to disable automatically playing movies when they are loaded. Defaults totrue. |
continuous |
Set this true to enable "continuous" galleries. By default, the galleries will not let a user go before the first image or after the last. Enabling this feature will let the user go directly to the first image in a gallery from the last one by selecting "Next". Defaults to false. |
counterLimit |
Limits the number of counter links that will be displayed in a "skip" style counter. If the actual number of gallery elements is greater than this value, the counter will be restrained to the elements immediately preceding and following the current element. Defaults to 10. |
counterType |
The mode to use for the gallery counter. May be either "default" or "skip". The default counter is a simple "1 of 5" message. The skip counter displays a separate link to each piece in the gallery, enabling quick navigation in large galleries. Defaults to "default". |
displayCounter |
Set this false to hide the gallery counter. Counters are never displayed on elements that are not part of a gallery. Defaults to true. |
displayNav |
Set this false to hide the gallery navigation controls. Defaults to true. |
enableKeys |
Set this false to disable keyboard navigation of galleries. Defaults to true. |
fadeDuration |
The duration (in seconds) of the fade animations. Defaults to 0.35. |
flashParams |
A list of parameters (in a JavaScript object) that will be passed to a flash <object>. For a partial list of available parameters, see this page. Only one parameter is specified by default: bgcolor. Defaults to {bgcolor:"#000000"}. |
flashVars |
A list of variables (in a JavaScript object) that will be passed to a flash movie asFlashVars. Defaults to {}. |
flashVersion |
The minimum Flash version required to play a flash movie (as a string). Defaults to"9.0.0". |
handleOversize |
The mode to use for handling content that is too large for the viewport. May be one of"none", "resize", or "drag" (for images). The "none" setting will not alter the image dimensions, though clipping may occur. Setting this to "resize" enables on-the-fly resizing of large content. In this mode, the height and width of large, resizable content will be adjusted so that it may still be viewed in its entirety while maintaining its original aspect ratio. The "drag" mode will display an oversized image at its original resolution, but will allow the user to drag it within the view to see portions that may be clipped. See the demofor a demonstration of all three modes of operation. Defaults to "resize". |
handleUnsupported |
The mode to use for handling unsupported media. May be either "link" or "remove". Media are unsupported when the browser plugin required to display the media properly is not installed. The link option will display a user-friendly error message with a link to a page where the needed plugin can be downloaded. The remove option will simply remove any unsupported gallery elements from the gallery before displaying it. With this option, if the element is not part of a gallery, the link will simply be followed. Defaults to "link". |
initialHeight |
The height of Shadowbox (in pixels) when it first appears on the screen. Defaults to 160. |
initialWidth |
The width of Shadowbox (in pixels) when it first appears on the screen. Defaults to 320. |
modal |
Set this true to prevent mouse clicks on the overlay from closing Shadowbox. Defaults tofalse. |
onChange |
A hook function that will be fired when Shadowbox changes from one gallery element to another. The single argument of this function will be the gallery element that is about to be displayed. |
onClose |
A hook function that will be fired when Shadowbox closes. The single argument of this function will be the gallery element that was last displayed. |
onFinish |
A hook function that will fire when Shadowbox finishes loading the current gallery piece (after all animations are complete). The single argument of this function will be the current gallery element. |
onOpen |
A hook function that will be fired when Shadowbox opens. The single argument of this function will be the current gallery element. Returning boolean false from this callback will prevent Shadowbox from opening. |
overlayColor |
The color to use for the modal overlay (in hex). Defaults to "#000". |
overlayOpacity |
The opacity to use for the modal overlay. Defaults to 0.5. |
resizeDuration |
The duration (in seconds) of the resizing animations. Defaults to 0.35. |
showOverlay |
Determines whether the overlay will be shown at all. Defaults to true. |
showMovieControls |
Set this false to disable displaying QuickTime and Windows Media player movie control bars. Defaults to true. |
skipSetup |
Set this true to skip automatically calling Shadowbox.setup when Shadowbox initializes. Defaults to true. |
slideshowDelay |
A delay (in seconds) to use for slideshows. If set to anything other than 0, this value determines an interval at which Shadowbox will automatically proceed to the next piece in the gallery. Defaults to 0. |
troubleElements |
An array of tag names of elements that should be hidden when a modal overlay is used. This option exists because some browsers are not able to display a semi-transparent overlay on top of these elements. These elements will have their visibility restored when Shadowbox closes. Defaults to ["select", "object", "embed", "canvas"]. |
viewportPadding |
The amount of padding (in pixels) to maintain around the edge of the browser window. Defaults to 20. |
Any of these options may be passed to Shadowbox.init in a JavaScript object, like this:
<link rel="stylesheet" type="text/css" href="shadowbox.css">
<script type="text/javascript" src="shadowbox.js"></script>
<script type="text/javascript">
Shadowbox.init({
handleOversize: "drag",
displayNav: false,
handleUnsupported: "remove",
autoplayMovies: false
});
</script>