My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
jQueryAPI  
Calling Slimbox 2 functions from Javascript
Phase-Deploy, Featured
Updated Feb 4, 2010 by christophe.beyls@gmail.com

Slimbox 2 works as a jQuery plugin.

Launching Slimbox directly from Javascript code

jQuery.slimbox()

You can launch Slimbox 2 using Javascript to display a single image or a group of images.

Single image

Call the jQuery.slimbox() function with the following parameters:

jQuery.slimbox(url, description, options);
  • url is mandatory, it's the URL of the image to display.
  • description is optional, it's the image description text.
  • options is optional, it's a javascript object where the keys are option names and the values depend on the key. The options are described in detail in the Slimbox manual. Example of options object: { captionAnimationDuration: 1, loop: true, overlayOpacity: 0.5 }

Examples:

jQuery.slimbox("tree.jpg");
jQuery.slimbox("http://externalwebsite.com/tree.jpg", "My beautiful tree");
jQuery.slimbox("tree.jpg", "", {overlayOpacity: 0.2});

Multiple images

Call the jQuery.slimbox() function with the following parameters:

jQuery.slimbox(images, startImage, options);
  • images is mandatory, it's an array of arrays. The main array contains one array for each image. The second-level arrays contain the image URL as a mandatory first element, and the image description as an optional second element. Example, for 2 images: [["image1.jpg"], ["image2.jpg", "description2"]]
  • startImage is mandatory, it's the zero-based index of the image that you want to show first when Slimbox opens. 0 is the first image of the array, 1 is the second, etc. Of course, this value must be smaller than the size of the main array. Most of the time, you will use 0.
  • options is optional, it's the same parameter as the one described above for displaying a single image.

Examples:

jQuery.slimbox([["cat.jpg", "Nice cat"], ["dog.jpg"]], 0);
jQuery.slimbox([["left.jpg"], ["middle.jpg"], ["right.jpg"]], 1, {loop: true});

Enabling HTML DOM elements (such as links) to launch Slimbox when clicking

The autoload code block

The autoload code block is the readable code block located by default inside the slimbox2.js file of the official Slimbox 2 distribution. This code is using the Slimbox jQuery API to register Slimbox for a specific kind of links, so that it will open when the user clicks on any of these links. The registration is performed as soon as the DOM is ready.

The default implementation mimics the original Lightbox behavior: it registers Slimbox for all links (<a> tags) having a rel attribute value starting with the word "lightbox". If the rel attribute contains only the word "lightbox", it opens Slimbox to display a single image. Otherwise, it displays an image gallery containing all the images having the same rel attribute value as the one that was clicked, like "lightbox-animal" for example. For each link, its target (href attribute) is used as the image URL and its title attribute is used as an optional description shown in a caption below the image.

All the aspects of this default implementation may be freely changed to fit your needs thanks to the powerful Slimbox jQuery API. The following section explains how to do it.

The slimbox() function on jQuery objects

Slimbox makes a new function available to objects created by the jQuery function. This slimbox() function will usually be called for link elements (<a> tags) but it's not mandatory: for example you can also use image maps (<area> tags). For other kinds of DOM elements, you'll need to provide a linkMapper function (described further in this page).

When calling this function, Slimbox will register itself and will open as soon as one of these DOM elements is clicked. The syntax is as follows.

jQuery(expression).slimbox(options, linkMapper, linksFilter);

parameters are all optional:

  • options is a javascript object where the keys are option names and the values depend on the key. The options are described in detail in the Slimbox 2 manual.
  • linkMapper is a function. It is described further in this page. If you want to omit this parameter, you can set it to null.
  • linksFilter is a function. It is described further in this page.

This is the magic part: calling the slimbox() function on a jQuery object which actually selects more than one element will cause the associated images to be shown together as a group in Slimbox, with one image for each DOM element. The order of the images in the Slimbox gallery will follow the order of the DOM elements in the group.

Examples:

Single element:

<a id="myLink" href="tree.jpg">Click Me</a>
jQuery("#myLink").slimbox();

Multiple elements:

<div id="gallery">
   <a href="tree.jpg">A tree</a>
   <a href="cat.jpg" title="Nice cat">A cat</a>
   <a href="dog.jpg" title="Look at this dog">A dog</a>
</div>
jQuery("#gallery a").slimbox();

The linkMapper function

The linkMapper is a function that you can pass as an optional parameter to the slimbox() function. It is responsible for doing the mapping between a DOM element and the image URL and description that will be shown in Slimbox when clicking on that element.

You can change it in order to change the location where the URL and description will be retrieved, or even customize the URL or description itself.

It takes one parameter: a DOM element.

It returns an array containing 2 elements:

  • The image URL.
  • The image description. It may be null, an empty string or even omitted because the description is optional.

When you don't provide a linkMapper function, a default one is used. Here is it:

function(el) {
	return [el.href, el.title];
}

As you can see, the default linkMapper uses the href attribute of the DOM element as image URL, and the title attribute of the DOM element (if present, otherwise it will return null) as the image description.

This default implementation works well with <a> and <anchor> tags. If you call the slimbox() function on other kinds of DOM elements which do not have an href attribute, of course it will not work and you will need to override the default linkMapper function.

Example 1:

Let's say that you want to fetch the image description from the alt attribute located in a DOM image node which is a direct child of the link node, instead of the title attribute of the link node itself. The HTML code looks like this:

<a href="flower.jpg" id="mypicture"><img src="flower-thumbnail.jpg" alt="Beautiful flower" /></a>

Here's the linkMapper function that you will need to use:

function(el) {
	return [el.href, el.firstChild.alt];
}

Please note that this function will only work if the DOM element actually has a child node, otherwise an error will occur when trying to access the title attribute.

Example 2:

This time, let's say that you want to use the image URL itself as description, so no need to fetch it from the title attribute of the DOM element. But you only want to keep the main part of the image URL, without the path at the beginning and without the file extension (.jpg). You will use a regular expression to achieve this. Here's what your custom linkMapper function will look like:

function(el) {
	return [el.href, /[^\/]*(?=\.\w+$)/.exec(el.href)];
}

Example 3 (Flickr integration):

In this last example, we'll accomplish a complete Flickr integration with very little code. Let's say that we have links pointing to pages dedicated to pictures on the Flickr website. Each one of these links is surrounding an <img> tag, which is actually a thumbnail of the picture hosted on the Flickr website. It looks like this:

<a href="http://www.flickr.com/photos/14516334@N00/345009210/" 
title="A bee"><img src="http://farm1.static.flickr.com/159/345009210_1f826cd5a1_t.jpg" alt="" /></a>

By default, clicking on this link (and thus on the thumbnail) will just make the browser navigate to the Flickr page of the picture.

Now, without changing anything to the HTML, we want to register Slimbox on these links so that when the visitor clicks on them, Slimbox will open and display the medium-sized image, also hosted on the Flickr website.

How to achieve this using a linkMapper function? It's quite simple actually. URLs of images hosted on the Flickr website are always the same for the same picture, only the end of the URL changes depending on the image size. Square-sized images name ends with _s.jpg, thumbnails name ends with _t.jpg, small images name ends with _m.jpg and medium-sized images name just ends with .jpg. So, we need to retrieve the URL of the thumbnail image which is a direct child of the clicked link, and then using a regular expression, replace the end of this URL with ".jpg" to get the URL of the medium-sized image. Done!

But that's not all, we also want to automatically add a link to the Flickr page of the picture in its description inside Slimbox. This is easy to achieve: we just need to append some HTML at the end of the existing description.

Last but not least, we'll filter out the link elements using a jQuery selector, only keeping those pointing to the Flickr webpages and surrounding a thumbnail image.

Here's the code:

$("a[href^='http://www.flickr.com/photos/'] > img:first-child[src]").parent().slimbox({}, function(el) {
	return [el.firstChild.src.replace(/_[mts]\.(\w+)$/, ".$1"),
		(el.title || el.firstChild.alt) + '<br /><a href="' + el.href + '">Flickr page</a>'];
});

The linksFilter function

The linksFilter is an optional function that you can pass as an optional parameter to the slimbox() function when called on a group of DOM elements. It allows you to exclude specific elements of the group when clicking on a particular element, so they will not be part of the selection of images that will be displayed by Slimbox when it opens after the click. The element that was clicked must never be excluded because it's the one that will be displayed first when Slimbox opens.

In other terms, this function allows you to divide a group into sub-groups based on a selected criteria, by hiding some images depending on the element that was clicked.

It takes one parameter: a DOM element.

Also, the DOM element that was clicked is accessible by using the this keyword from inside the function.

It returns a boolean: true if the element that was passed as a parameter must be part of the selection of images that will be displayed, false if the element must be excluded. When the passed DOM element is equal to this, the function must return true.

When you don't provide a linksFilter function, a default one is used. Here is it:

function(el) {
	return true;
}

which means that by default no DOM element will ever be filtered out. There will only be one big group.

Example 1:

Let's say that you want to display all the images having the word "exclusive" somewhere in the title attribute value of their DOM element together in a separate "exclusive" group, and all other images together in a default group. Here is the linksFilter function that you will use:

function(el) {
	return /exclusive/i.test(this.title) == /exclusive/i.test(el.title);
}

Example 2:

This is a very useful one. Let's say that you want to automatically group together all the images with a DOM element having the same parent node (for example, all image links inside the same <div> block or the same paragraph). Images with a DOM element having no parent node or being a "unique slimbox child" will be displayed individually. Here is the linksFilter function that achieves this:

function(el) {
	return (this == el) || (this.parentNode && (this.parentNode == el.parentNode));
}

As you can see, we need to test first if the parent node exists before comparing the parent nodes of the clicked element and the passed element, otherwise all orphans will be grouped together. If there is no parent node, we return false so that all images are excluded, except for the one which was clicked for which we always return true.

Example 3 (Lightbox behavior):

The default autoload code block provided with Slimbox makes use of the following linksFilter function to mimic the default behavior of Lightbox:

function(el) {
	return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
}

It may look a bit complicated at the beginning but it's not. Here's the detailed explanation.

  • Before calling this linksFilter function, we have already narrowed our selection of DOM elements to the ones having a rel attribute value starting with the word "lightbox".
  • First, if the clicked element (this) is the one being tested, we return true as required. It will always be part of the selection.
  • If it's not, then we test if the rel attribute value has exactly 8 characters. If it has, it means that the rel attribute value is exactly "lightbox" (8 letters) and in this case we return false so that in the end all images will be filtered out, except for the one that was clicked of course. This allows to display the image individually if the rel attribute value is exactly "lightbox".
  • Finally, if the rel attribute value has more than 8 characters, we know that it's a group name (like "lightbox-animals" for example) and we return true far all DOM elements having exactly the same group name as the one being clicked, or false to discard the other ones. This allows to display all the images having the same group name together.

Unregistering Slimbox from the DOM elements

In order to reverse the process and unregister Slimbox once the slimbox() function has been called on a jQuery object representing a selection of DOM elements, use the following code on the same jQuery object.

jQuery(expression).unbind("click");

The normal behavior will then be restored for these DOM elements and clicking on them will not launch Slimbox anymore.

Comment by bjorsq, May 7, 2009

Hi Christophe,

I used to use a modified version of Lightbox on a site and have been converting it to use slimbox instead. I need to use your linkMapper function to access the image src attribute, but had some problems. el.firstChild pointed to a text node (whitespace), so I needed to wrap it in jquery to access it - i.e.

linkMapper = function(el) {
    return [$('img',el).attr("src").replace('/thumbnails',''), el.title];
}

In my case, all the images I use are kept in directories within a main image directory, and all thumbnails are stored in a thumbnails sub-directory of this.

Comment by xidades...@gmail.com, Aug 16, 2009

how can I close the slimbox? like when the user presses the close button.

Comment by pferdefl...@gmail.com, Oct 11, 2009

it works for me to close it by just calling the jQuery.slimbox("yourImage.jpg") function again...

I am having an issue where my swf (that I have loaded with either Swiff() with the mootools version or with the jquery.flash() extension with this version) is being made invisible(it keeps running but is gone) by my slimbox. when my slimbox goes away the swf comes back. weird...?

Comment by rajs...@gmail.com, Oct 18, 2009

@pferdefleisch It sounds like what you are saying is actually a feature of the script (from the homepage): "It temporary hides flash animations and other embedded videos while it is open to avoid display bugs in some browsers. It also hides select boxes in Internet Explorer 6."

Comment by pec_eve...@putney.net, Nov 19, 2009

Can I add html links inside the title section like I have done with prototype lightbox?

Comment by gyamo...@gmail.com, Dec 2, 2009

How to manage photos on differents folders? eg the html index is folder A, with links to folders B and C which contains two sets of photos.

Comment by planetha...@gmail.com, Dec 28, 2009

How do I add a link inside of the caption?

Comment by tfs...@gmail.com, Feb 15, 2010

@source http://www.digitalia.be/software/slimbox2 "You can even use HTML in the caption if you want. You must replace the < and > characters with HTML entities and use single quotes instead of double quotes."

So you can add link. You can also change the way the caption is loaded by setting your custom linkMapper. I made that :

function linkMapper(el) {
// defaut is : return [el.href, el.title];
return [el.href, $('#'+$('span',el).html()).html()];
}

But beware if you have a a link then it will be loaded. I have to find a hack to correct this.

Comment by nickjh...@gmail.com, Feb 28, 2010

This is probably a really dumb question but I cannot figure out what to do here. Is there a way that I can call slimbox2 after the page has loaded? Reason being I am pulling in a bunch of photos from flickr. I then want to display those photos in slimbox. Problem is, slimbox doesn't work because the flickr photos are added to the page after slimbox is called.

Basically, I would like to wrap a function in this:

$(window).bind('load', function() { });

But what do I even call? Just calling "jQuery.slimbox()" won't do it since I am not passing in images yet. They are dynamic. Thoughts?

Thanks Nick

Comment by nickjh...@gmail.com, Feb 28, 2010

Actually I just figured that out. I needed this:

$(window).bind('load', function() { $("#Flickr a").slimbox(); });

However, that doesn't work as I expected. That means that only the first image in the list works. How do I need to call this function? Do I need to do it every time an anchor tag is clicked since the images are loaded in dynamically?

Thanks Nick

Comment by papoga...@gmail.com, Mar 14, 2010

How do you add Next and Prev to the bottom caption of the slimbox? Also the ability to add a custom link?

Comment by oliver.o...@gmail.com, Mar 25, 2010

Slimbox2 works great. I use it to open an image via javascript. However as the image loads, my page scrolls back to top and i have to scroll back down to see the image. I cant figure out where the bug is. Has someone encountered this before?

Comment by elmo...@gmail.com, Mar 25, 2010

I am having a problem where slimbox2 hides a SWF on my page but it never comes back after slimbox2 is closed.

Comment by elmo...@gmail.com, Mar 25, 2010

If I remove the code in slimbox2 that hides the SWF and instead set the SWF's wmode to transparent, all is well. Can you please add an option to disable that function?

Comment by elmo...@gmail.com, Mar 25, 2010

I also need to disable slimbox temporarily, then re-enable it. I use

$("a[rel^='lightbox']").unbind("click");

to disable it, but I am unable to re-enable it, even if I re-execute the autoload block.

Comment by phelu...@gmail.com, Apr 17, 2010

jQuery.slimbox(newtempimage, 0,{loop: true});

here newtemimage is an array with values: image.gif, image2.gif

tis does not work can u help me out please?

Comment by asbr...@gmail.com, May 12, 2010

Hey, i can't get flickr integration working on my site, could someone help me? i don't know why but the slideshow "shows" partials flickr photos on the bottom left of the webpage..

http://www.foglia-verde.it/

Thanks for the support.

Comment by sdiv...@gmail.com, Jun 25, 2010

Any way to display inline html content within slimbox?

Comment by rogad...@gmail.com, Aug 4, 2010

Just thought I would let you know that slimbox does not work with IE8+Prototype+Transistional DTD. It doesn't seem to pass the parameters using the jQuery.slimbox("tree.jpg"); method assigned to an onclick event.

Slimbox appears to fire up but it shrinks to a small block and there is no image.

Comment by iobot...@gmail.com, Nov 4, 2010

I had a problem for a costum implemantation where you could open the first image and if you go to the next or previous, it would freeze. The same if i close the first image and try to open a new one. I resolved it by adding the code $(center).dequeue(); exactly after $(center).queue(function() {...etc...}. It was a costum implemantation and i used my own attributes for this(not rel='lightbox' and href='...').

Comment by leogerm...@gmail.com, Nov 9, 2010

Hi,

Is there a elegant way to dynamically change the caption link? My case is: Im using it with WordPress?, and the images displayed on slimbox are one of the auto generated thumbnails, and I want the caption to link to the full size image.

cheers

Leo,,

Comment by JRitterb...@gmail.com, Nov 11, 2010

I'm running into a problem that I think iobotis4 is talking about but I do not follow the fix above. I am trying to add Slimbox to a Google Map by using an onclick in a link along with a call to slimbox

jQuery.slimbox("tree.jpg");

When I click on this link the Slimbox appears like it should, but once I click close and try to open another one it just displays a blank square at the default size (250x250). It puts the load gif for one second but then just stops.

I assume the .dequeue() mentioned by iobotis4 above clears something out to allow a new image to be called, but I don't see any code that matches what he lists above.

Any ideas on how to fix this?

Comment by m...@harp1.com, Dec 24, 2010

Thanks for the terrific tool and doc. Question anyone, please: is it possible to allow clicking on the image in order to close it? I'm displaying single images and some are large and require scrolling down to find the Close. It's not obvious to some to click outside the image to close or to press one of the exit keys. Thank again!

Comment by fox...@gmail.com, Feb 3, 2011

I have some links set up like this: <div>

<div>
<a rel="lightbox[set_1]" href="image.jpg"></a>
</div> <div>
<a rel="lightbox[set_1]" href="image.jpg"></a>
</div>
</div>

<a>I want to call slimbox with this link</a>

So is there anyway I can call slimbox for those images from that last link? I havn't been able to figure it out with this documentation.

Comment by jimbeni...@gmail.com, Feb 11, 2011

I'm trying to prevent slimbox from opening images in a carousel unless it is the front focussed image. I can disable regular links using the jQuery preventDefault() function. Doesn't work for slimbox though.

Open to ideas

Comment by jaboc...@gmail.com, Apr 15, 2011

Hi, I'm trying to use Slimbox as a modal window for non-images, to present textual information when a link is clicked.

Is this possible?

If so can someone please point me at an example of this, or an idea of how to go about it.

Thanks

Comment by p...@replete.nu, May 26, 2011

There are extra features all mentioned in these comments, that would be really good if integrated into slimbox.

I'm moving house over the next week, but after that, if nothing has happened I will fork the code somewhere (if allowed) and make the changes myself.

Phil

Comment by bradha...@gmail.com, Jun 28, 2011

I'm using the front page slideshow in joomla and would like to link from one of the slides to a swf(right now, i'm just trying to get an image to work). I only have a dialog box to add the href, so I'm trying to see if i can launch the image in a slimbox as opposed to linking to a page like the rest of the slides. So i enter:

javascript:jQuery.slimbox('');

and i get the "false" return. I did review the above regarding the linkFilter function, but a little beyond me to understand. Any fixes for what I'm trying to do? (and my apologies for "scrubbing" the link, client's site is not live yet, and for legal reasons i can't put domain out there).

Thank you for any help. brad

Comment by robra...@gmail.com, Jun 29, 2011

Flickr has added a new medium 640 size image (if I', not mistaken, t=thumbnail, s=small, etc. The new size is z which is the 640 pixel size. I can't figure out how to change slimbox from the 500 pixel medium to the newer larger 640 z size. Anybody know?

Comment by robra...@gmail.com, Jun 29, 2011

In case it matters, I'm using slimbox 2.

Comment by solstice.dhiver@gmail.com, Sep 13, 2011

the default autoloader force us to use a non valid rel attribute.

for html5, I use custom data attribute (look at http://html5doctor.com/html5-custom-data-attributes/) with this autoload block

if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	jQuery(function($) {
		$("a[data-lightbox]").slimbox({/* Put custom options here */}, null, function(el) {
			return (this == el) || ($(this).attr('data-lightbox') == $(el).attr('data-lightbox'));
		});
	});
}

example

<a href="myimage.jpg" data-lightbox=""><img ...></a>

and for a group

<a href="1.jpg" data-lightbox="number"><img...></a>
<a href="2.jpg" data-lightbox="number"><img...></a>
Comment by rgtur...@ameritech.net, Oct 7, 2011

For multiple images the example shows jQuery.slimbox([["left.jpg"], ["middle.jpg"], ["right.jpg"]], 1, {loop: true});

Can this be done from an array? Something like this jQuery.slimbox(o, 1, {loop: true});

What should the o array look like for this to work?

Comment by rumahjep...@gmail.com, Nov 16, 2011

your blog is very nice information for me.... visit my blog http://www.rumahjepara.com/ thanks

Comment by agussti...@gmail.com, Feb 3, 2012

Wah Mantab infonya Boz. thanks before visit at our web company http://www.tokojati.com/ Yuk tukeran link Boz............

www.tokojati.com

<a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com"> Toko Jati <a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com"> Tokojati.com</a> <a href="http://www.tokojati.com">pusat mebel jati jepara berkualitas </a> <a href="http://www.tokojati.com">pusat mebel jati jepara berkualitas </a> <a href="http://www.tokojati.com">pusat mebel jati jepara berkualitas </a>

Comment by agussti...@gmail.com, Feb 3, 2012

www.tokojati.com adalah pusat belanja mebel jepara online dengan cara mudah berbelanja furniture mebel dan ukir jepara serta souvenir jepara dengan harga murah,kualitas dan keamanan berbelanja mebel jepara sangat kami prioritaskan. kami melayani pembelian grosir dan retail, jaminan keamanan dan kenyamanan bertransaksi kami jaga dengan baik. produksi kerajinan kami dengan menggunakan bahan dasar kayu jati pilihan, mudah murah berkualitas itu moto kami. Silahkan lihat-lihat koleksi kerajinan kami .di www.tokojati.com atau jendelainformasi.info , jika barang yang anda cari belum ada pada koleksi furniture kami silakan kontak kami atau email kami. Jika anda tertarik untuk membeli produk kami, kami harap anda tidak sungkan-sungkan untuk menanyakan harganya via telepon, sms / email. terima kasih...

Comment by dwdws1...@gmail.com, Feb 4, 2012

This site explains how to show prev/next as default rather than on hover over L/R part. I recall trying this a while back in Lghtbox 2:

#lbPrevLink {left:0;background:transparent url(prevlabel.gif) no-repeat 0% 15%;} #lbPrevLink:hover {background:transparent url(prevlabel.gif) no-repeat 0% 15%;}

#lbNextLink {right:0;background:transparent url(nextlabel.gif) no-repeat 100% 15%;} #lbNextLink:hover {background:transparent url(nextlabel.gif) no-repeat 100% 15%;}

From: http://forum.joomlaworks.gr/simple-image-gallery-pro-%28plugin%29/changing-look-and-feel-or-slimbox-lytebox-or-litebox/


Sign in to add a comment
Powered by Google Project Hosting