|
jQueryAPI
Slimbox 2 works as a jQuery plugin. Launching Slimbox directly from Javascript codejQuery.slimbox() You can launch Slimbox 2 using Javascript to display a single image or a group of images. Single imageCall the jQuery.slimbox() function with the following parameters: jQuery.slimbox(url, description, options);
Examples: jQuery.slimbox("tree.jpg");jQuery.slimbox("http://externalwebsite.com/tree.jpg", "My beautiful tree");jQuery.slimbox("tree.jpg", "", {overlayOpacity: 0.2});Multiple imagesCall the jQuery.slimbox() function with the following parameters: jQuery.slimbox(images, startImage, options);
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 clickingThe autoload code blockThe 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 objectsSlimbox 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:
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 functionThe 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:
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 functionThe 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.
Unregistering Slimbox from the DOM elementsIn 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. |
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.
how can I close the slimbox? like when the user presses the close button.
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...?
@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."
Can I add html links inside the title section like I have done with prototype lightbox?
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.
How do I add a link inside of the caption?
@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 :
But beware if you have a a link then it will be loaded. I have to find a hack to correct this.
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
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
How do you add Next and Prev to the bottom caption of the slimbox? Also the ability to add a custom link?
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?
I am having a problem where slimbox2 hides a SWF on my page but it never comes back after slimbox2 is closed.
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?
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.
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?
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.
Any way to display inline html content within slimbox?
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.
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='...').
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,,
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?
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!
I have some links set up like this: <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.
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
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
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
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
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?
In case it matters, I'm using slimbox 2.
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
and for a group
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?
your blog is very nice information for me.... visit my blog http://www.rumahjepara.com/ thanks
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>
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...
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/