You will recieve your password to this address. Address is not made public.

Your preferred username that is used when logging in.

jQuery Tools / Overlay - A generic tool for overlaying needs

For promotional purposes

Berlin Gustavohouse

The Gustavo House in Storkower Strasse. It was built in 1978 and reconstructed in 1998 by the Spanish artist Gustavo.

Berlin Alexanderplatz Station

Berlin Alexanderplatz is a railway station in the Berlin city centre and is one of the city's most important interchange points for local public transport.

For image galleries

For user interactions

For every overlaying need

Overlaying HTML on top of the main document is one of the most common features in today's websites. You need different kinds of overlays for different situations such as:

  1. Great looking overlays for displaying your products.
  2. Simple and snappy overlays for showing info or warning boxes.
  3. Modal dialogs for prompting and alerting users.
  4. "lighbox" style image galleries.

This tool handles all these different situations by offering a flexible effect framework. You can use the effect that is most suitable for the situation at hand.

The overlay can contain any HTML and the whole thing can be styled with CSS. There is no clutter and there are no surprises. This overlay tool is now a significant part of the JavaScript/Web 2.0 landscape. Don't rely on other, half-baked solutions.

This tool uses a simple and natural syntax and has an advanced programming API. You can extend it with callback methods or with your own plugins. This tool has a similar structure and feel as the other tools. Learn to use this tool and you will know how to use the rest.

This tool is a mature project and is actively maintained. The source code is clean, professional and easy to follow. It passes Douglas Crockford's JavaScript Verifier and it it doesn't assign any global variables. Active forums keep this tool alive and you can expect to see new releases coming out in the future.

Usage

You can study the workings of overlay in the following examples. Each contains documented source code, along with a standalone page to allow you to easily transfer the examples to your own site. The most important example is the first one, "Minimal setup for overlay". It teaches you the basics of using the library.

Combining overlay with Flowplayer

These demos provide some examples of how to use overlay and Flowplayer together.

overlay Graphics

Download tooltip graphics

You can use our graphics as the basis for your design. You can freely change the design as you see fit. Click the image on the right to download a zip file. Before using the graphics, you should consult the User's Guide on how graphics can be used when designing the look and feel of the tools.

Here are a few examples of what is included in the zip file:


White overlay White overlay White overlay

Configuration

The general way to setup overlay is as follows:

// select one or more elements to be overlay triggers
$("JQUERY SELECTOR").overlay({

	// one configuration property
	color: '#ccc',

	// another property
	top: 50

	// ... the rest of the configuration properties
});

Here is a list of all available configuration options.

Property Default value Description
absolute false If set to true, the overlay position is measured from the top-left corner of the browser window and the scrolling position has no impact. By default, the scrolling position is added.
api false When this tool is initialized (constructed), the return value is a jQuery object associated with the selector. By setting this property to true the return value is this tool's JavaScript API instead. If the selector returns multiple elements, the API of the last element will be returned. Read more about accessing the tool API.
close A jQuery selector for the closing elements inside the overlay. These can be any elements such as links, buttons or images. If this is not supplied, a close element is auto-generated. Read more about this defining close actions.
closeOnClick true By default, overlays are closed when the mouse is clicked outside the overlay area. Setting this property to false suppresses this behaviour which is suitable for modal dialogs.
closeOnEsc true By default, overlays are closed when the ESC keyboard key is pressed. Setting this property to false suppresses this behaviour.
effect 'default'

The effect to be used when an overlay is opened and closed. This can dramatically change the behaviour of the overlay. By default this tool uses an effect called "default" which is a simple show / hide effect.

Here is a list of currently available effects and you can also make your own effects.

expose Overlay is very often used together with the expose tool. Because of this, the support for this feature has been built inside the tool. This configuration accepts the expose tool's configuration. This is either a simple string specifying the background color of the mask or a more complex object literal specifying more configuration variables.

See an example of an overlay together with expose. By default the expose feature is disabled.

left "center" Specifies how far from the left-hand edge of the screen the overlay should be placed. By default the overlay is horizontally centered with the value "center" but you can also supply a numerical value specifying a distance in pixels.
oneInstance true By default, there can be only one overlay on the page at once. Setting this property to false allows you to have multiple overlay instances.
speed 'normal' The speed of the fade-in animation on the "default" effect. Valid values are 'slow', 'normal' and 'fast', or you can supply a numerical value (in milliseconds). By setting this property to 0, the overlay will appear immediately without any animation.
target The element to be overlayed (if not specified in the rel attribute of the triggering element).
top '10%' Specifies how far from the top edge of the screen the overlay should be placed. Acceptable values are an integer number specifying a distance in pixels, a string (such as '15%') specifying a percentage value or "center" in which case the overlay is vertically centered. Percentage values work consistently in different screen resolutions.
onBeforeLoad None This callback function is triggered before the overlay is displayed. A function which returns false or calling event.preventDefault will prevent a particular overlay from being loaded.
onLoad None This callback function is triggered when the overlay has completely been shown and a particular overlay is in place.
onBeforeClose None This callback function is triggered before a particular overlay is closed. A function which returns false or calling event.preventDefault will prevent a particular overlay from being closed.
onClose None This callback function is triggered when a particular overlay is closed.
onStart None Since 1.1.2. This callback function is triggered just before the overlay starts appearing in an animated way. This happens between the onBeforeLoad and onLoad events. The overlay has already been positioned at the location from where it will start animating. This callback is not triggered if the loading action has been cancelled in onBeforeLoad.

Using callback functions

Here is an example of an onLoad callback function. Inside any callback function the this variable is a pointer to the overlay scripting API:

$("button[rel]").overlay({

	// hide trigger when the overlay is opened
	onLoad: function(event) {
		this.getTrigger().hide();
	}
});

See the Using Callback Functions section in the User's Guide for more detailed dscription about this important topic.

Defining custom close elements

By default, a close button is auto-generated as the first element inside the container. Here is the generated HTML:

<!-- auto- generated close button -->
<div class="close"></div>

You can easily add more closing elements inside the overlay simply by assignin the CSS class name "close" to them. These elements can be styled and positioned any way you like inside the overlay.

If you supply a value for the close configuration variable, the close element is not auto-generated and you need to define the closing element(s) yourself.

Scripting API

Overlay has a clean and well-designed API for scripters, with which it is possible to enrich the overlay user experience. You can get a handle to the API in numerous ways as described in the Users Manual. Here is an example of how to access the API:

// get handle to the API upon initialization by enabling the "api" variable
var api = $("div.overlay").overlay({oneInstance: false, api: true});

// open the overlay programatically with this API call
api.load();

// retrieve an existing api
api = $("div.overlay").overlay();

Here is a list of all available API methods:

Method Return value type Description
close() Overlay Closes the overlay.
getClosers() jQuery Returns the closing element(s) as a jQuery object.
getConf() Object Returns the configuration for the overlay.
getOverlay() jQuery Returns the overlayed element as a jQuery object.
getTrigger() jQuery Returns the triggering element as a jQuery object.
isOpened() boolean Returns true if the overlay is opened.
load() Overlay Opens the overlay.
onBeforeLoad(fn) API Registers a new callback function that is triggered before the overlay is shown. The callback is given as the argument. Returning false or calling event.preventDefault in the callback will prevent the overlaying from taking place. You can assign multiple onBeforeLoad listeners with this command.
onLoad(fn) API Registers a new callback function that is triggered right after the overlay has been loaded. The callback is given as the argument. You can assign multiple onLoad listeners with this command.
onBeforeClose(fn) API Registers a new callback function that is triggered before the overlay is closed. The callback is given as the argument. Returning false or calling event.preventDefault in the callback will prevent the overlay from closing. You can assign multiple onBeforeClose listeners with this command.
onClose(fn) API Registers a new callback function that is triggered after the overlay has been closed. The callback is given as the argument. You can assign multiple onClose listeners with this command.
bind(names, fn) API Register one or more callbacks to be triggered. See using callbacks for more information.
unbind(names) API Removes callback functions from the tool See using callbacks for more information.

Making custom effects

If you want to make custom effects you should use the $.tools.overlay.addEffect method. This method is "static", meaning that you don't have to have the the overlay API (or instance) already loaded. You can add effects before any overlays are initialized.

This method accepts three arguments. The first argument is the effect name, the second argument is the function that performs the required functionality for loading the overlay and the third argument is a function that closes the overlay. You can use this method to modify existing effects or add new ones. Here is the official default effect taken directly from the source code of the overlay tool:

// adds an effect called "myEffect" to the overlay
$.tools.overlay.addEffect("myEffect",

	// load function
	function(done) {

		/*
			- the 'this' variable is a reference to the overlay API
			- here we use jQuery's fadeIn() method to perform the effect
		*/
		this.getOverlay().fadeIn(this.getConf().speed, done);
	},

	// close function
	function(done) {

		// fade out the overlay
		this.getOverlay().fadeOut(this.getConf().closeSpeed, done);
	}
);

As you can see it is fairly easy to implement effects. Inside the functions the this variable is a pointer to the scripting API. This gives you access to various parts of the overlay elements. The functions are fed with one argument which is a reference to a function that must be called after you have performed you effect. The reason for this is that many times those effects perform animations that have a certain duration. We must know when these animations are finished so that the onLoad and onHide events are really called after the overlay is completely shown or hidden.

Note that if you are using jQuery's animate, fadeIn or fadeOut methods the done function can simply be given to this method as the last argument. jQuery will take care of it after the animation finishes. Otherwise you must call done.call().

Here is one custom overlay effect demo.

Effects

Apple Effect

The Apple effect looks and behaves like you might have seen on apple.com. Click here to see an example:

Overlaying is done in a visually appealing way by growing a background image from 0 to the desired size. If you choose to use this effect you are tied to the dimensions of the image. You can change the width but the height changes accordingly so that the aspect ratio remains the same.

Usage

The best way to learn is to study the full documentation for the example above. Here you can see the JavaScript setup for this effect:

$(function() {
	$("#apple img[rel]").overlay({effect: 'apple'});
});

It's important to note that this effect is not included in the standard distribution of jQuery Tools. You need to include it yourself on the download page.

You may want to configure this effect as a default effect so that you don't have to define the effect property on every call. This can be achieved with global configuration.

Configuration

Here is a full list of available configuration options for this effect. These options are added to the default configuration options.

property default description
start (Object) {
  top: null,
  left: null,
  absolute: false
}
The starting position and dimensions of the animated image. By default, the starting position is the position of the element that triggers the overlay and typically you don't need to configure this option.

When the overlay is loaded programmatically with the load() API call, the default starting position is the centre of the screen. You can override these defaults using the following properties:

  • top specifies how far from the top edge of the screen the growing animation should start.
  • left specifies how far from the left-hand edge of the screen the growing animation should start.
  • absolute By default this is set to true so the starting position is measured from the top left-hand corner of the browser window and the scrolling position has no impact. By default the scrolling position is added.
fadeInSpeed 'fast' The speed at which the content fades in on top of the background image after the growing animation has completed. Valid values are 'slow', 'normal' and 'fast', or you can supply a numerical value (in milliseconds).
zIndex 9999 This is the CSS z-index property for the background image. A z-index specifies the placement of an element on a document's z-axis. This value must be greater than the z-index of any other element on the page so that the overlay will always be on top of other elements on the page. Note: If you are using the expose effect together with the overlay the zIndex of the expose mask is 9998 by default.

Using the Apple effect upon page load

Due to a major bug in jQuery you should not load overlays with the Apple effect upon document.ready() event when using external stylesheets. This causes unpredictable behaviour in Safari 3+ and Chrome (webkit-based browsers). There are two workarounds for handling this:

Plugins

Gallery plugin for Overlay

Click on any of the following thumbnails and you will realize that you don't need any other overlaying solution anymore.

Highlights

Usage

The best way to learn is to study the full documentation for the example above. For the impatient here is the HTML setup. Notice the standards based syntax.

<!-- list of thumbnails -->
<div id="thumbs">

	<a href="path/to/big-picture1.jpg" title="Image description #1">
		<img src="path/to/thumbnail1.jpg" /></a>

	<a href="path/to/big-picture2.jpg" title="Image description #2">
		<img src="path/to/thumbnail2.jpg" /></a>

	<a href="path/to/big-picture3.jpg" title="Image description #3">
		<img src="path/to/thumbnail3.jpg" /></a>

	<!-- and so on ... -->
</div>

JavaScript setup

// select the thumbnails and make them trigger our overlay
$("#thumbnails a").overlay({

	// each trigger uses the same overlay with the id "gallery"
	target: '#gallery',

	// optional exposing effect
	expose: '#f1f1f1'

// let the gallery plugin do its magic!
}).gallery({

	// the plugin accepts its own set of configuration options
	speed: 800
});

Configuration

Here is a full list of available configuration options. You can also see these options by running the command console.dir($.tools.overlay.plugins.gallery.conf); from the console of the Firebug plugin in Firefox. These options are given for the gallery plugin and not for the overlay itself.

property default description
activeClass 'active' The CSS class name for the trigger element which corresponds to the current image.
autohide true Specifies whether the next/prev buttons and the info box are automatically hidden. If this is set to false then these elements are always shown.
disabledClass 'disabled' The CSS class name for disabled next and prev elements. For example, the prev element is disabled when there are no previous items to scroll to.
imgId img The id of the image that will be generated inside the overlay.
info '.info' Selector for the nested element where the image information is placed.
next '.next' Selector for the nested element to which a "next image" action should be bound.
preload true Specifies whether the previous and next images are automatically loaded into the browser's cache.
prev '.prev' Selector for the nested element to which a "previous image" action should be bound.
progress '.progress' Selector for the nested element that works as the loading indicator. Currently an animated GIF is the only viable option but in the future this may be an animated PNG.
opacity 0.8 The transparency setting for tne next/prev and information elements. A decimal value from 0 to 1. A bigger value means less transparency while a value of 0 is fully transparent (invisible).
speed slow The speed of how fast the overlay is resized when the image is changed.
template A pattern that specifies how the information is formatted inside the info box. By default following string will be used
'<strong>${title}</strong> <span>Image ${index} of ${total}</span>'