Hi! First of all, my english is not so good: please be patient with my mistakes.
For anything wrong, as well as for debug questions and suggestions, you can try to send me an e-mail: I usually don't have much time, but I'll try to answer you.
You'll also find some posts in mootools users google group: http://groups.google.com/group/mootools-users/
As long as I'll have the time to do it, I'll try to help anyone who ask for some help with Milkbox implementation or for little customizations, but I'll definitely favour people that make a donation (see the bottom of this page).
Nothing special: just another lightbox clone, this time written for Mootools 1.2.x. I needed it, so I decided to write my own and I'm happy to share it.
Inspiration comes from THE VERY FIRST MAN ON THE MOON: Lokesh Dhakar, and his fabulous Lightbox: http://www.lokeshdhakar.com/projects/lightbox2/.
Swf support. You can add swf links to your galleries as well as images.
Use the rev attribute to tell width and height of the swf file.
AutoSize and maxHeight options. AutoSize is enabled by default; if needed, it makes photos smaller, so they'll fit the browser window. If you want them not to be higher then a certain height, just set the maxHeight option.
AutoPlay and autoPlayDelay options. You can set them globally or on a per-gallery basis, as well as in JavaScript calls.
XML galleries. Instead of writing HTML links and let Milkbox to automatically collect them, you can write one or more galleries via XML and load them with the addGalleries() method. You can then show any file of any gallery with a single JavaScript or ActionScript (Using ExternalInterface) call, and with custom auto-play options.
Launch from JavaScript. You can use showGallery() to launch any gallery item with custom autoplay options from JavaScript and therefore, even from Flash via ExternalInterface.
Milkbox hides select and textarea tags while playing; also, there's a option (removeTitle) that lets you decide if the browser will pop up or not his default tooltips when you hover the Milkbox links. Defaults to true.
ImageOfText option. If your lang is not eng, and don't want to see "1 of 15", just use this option. I'm italian, so I usually set it to 'di'. Defaults to 'of'.
Transitions are minimal: I like fast navigation. Anyway, you can use any of the MooTools's Fx.Transitions effects.
Next and prev buttons are in the bottom section, near the close button. This way I think I make it more usable by not hiding anything and keeping the navigation all in one place.
Also there's a loop in the next and prev buttons: if Milkbox is showing the last image in the gallery and you click the next button, it will let you see the first image; similarly if you're seeing the first one, the last one will be shown by clicking the prev button.
And last but not least, there's an auto-sizing border-left in the nav section, that helps to visually separate the buttons from the caption.
Keyboard navigation:
• Next image: right arrow or space bar
• Previous image: left arrow
• Close milkbox: esc key
The close button is just a cross, no 'close' word: this way no language is involved.
In fact, there are 3 ways to close it: click the close button; click the overlay; press the esc key.
All the graphic interface is customizable via CSS or js options. You can even download the buttons graphics (.psd and .ai) and modify them!
YUI compressed. The files you'll find in the download section, include a lighter yui-compressed version (milkbox-yc.js)
.To correctly see the flash animation, you need to install flash player 10.
![]() |
![]() |
![]() |
![]() |
First photo by Retinafunk
Below you will find some other examples.
Make sure that there is a XHTML DOCTYPE declaration at the very top of the page. XHTML transitional or strict are good.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Include the milkbox.css file in your page. Make sure the referenced images are in the right location. I suggest to leave them in the milkbox css folder.
<style type="text/css"> @import url(css/milkbox/milkbox.css); </style>
Include the mootools 1.2 js library. milkbox.js needs the following libraries to work properly:
<script type="text/javascript" src="js/mootools-1.2.3-core-yc.js"></script> <script type="text/javascript" src="js/mootools-1.2.3.1-more.js"></script>
Include the milkbox.js or (maybe better) the milkbox-yc.js file.
<script type="text/javascript" src="js/milkbox.js"></script>
Add a rel="milkbox" attribute to any link tag to activate the Milkbox. Optionally, add a title attribute.
If you have a set of related images that you would like to group, include a group name between square brackets in the rel attribute, or separate it from the 'milkbox' word with some other character: for example, if you'd like to validate your page as xhtml 1.1, you could use a colon or an underscore.
If you want to add tags inside the title property (for example a link), please use html entities and no quotes, then eventually style them with css: the relevant id is #mbDescription.
<a href="img/01b.jpg" rel="milkbox[gall1]" title="Caption goes here...">image #1</a> <a href="img/02b.jpg" rel="milkbox[gall1]" title="Caption goes here...">image #2</a> <a href="img/03b.jpg" rel="milkbox[gall1]" title="Caption goes here...">image #3</a>
Example for xhtml 1.1 validation - A colon instead of the square brackets:
<a href="img/01b.jpg" rel="milkbox:gall1" title="Caption goes here...">image #1</a> <a href="img/02b.jpg" rel="milkbox:gall1" title="Caption goes here...">image #2</a> <a href="img/03b.jpg" rel="milkbox:gall1" title="Caption goes here...">image #3</a>
If a link points to an swf file, add a rev attribute with comma-separated width/height values:
<a href="swf/banner.swf" rel="milkbox[gall1]" rev="width:600,height:200" title="Caption goes here...">image #1</a>
You can customize the following css properties by modifying the milkbox.css file (see comments inside) or the DOM elements via setStyle/setStyles. The relevant DOM elements are written in brackets:
You can also add border/padding to the images (or swf files) via the global JavaScript options.
To set some css property via JavaScript you can do something like this:
window.addEvent('domready', function(){
milkbox.center.setStyle('borderColor','#00ff00');
milkbox.overlay.setStyle('backgroundColor','#aabbcc');
});
Here's the list of the global options you can modify. Default values are written in brackets:
By modifying the global options, you change the options for all galleries in your page.
You can set them in 3 ways:
1. In the .js file, at the top of the class.
window.addEvent('domready', function(){
milkbox = new Milkbox({ resizeTransition:'back:out', topPosition:30 });
});
3. In a js script, after the DOM is ready, by using the changeOptions() method:
window.addEvent('domready', function(){
milkbox.changeOptions({
canvasBorderWidth:'5px',
canvasBorderColor:'#ffcc00',
canvasPadding:'10px'
});
});
The changeOptions() method is essentially a wrapper for the setOptions() method. It makes sure that all the options are really changed (especially transition and duration).
If you want for some reason to temporarily change the global options and then restore them with the originals, you can do it with the restoreOptions() method:
window.addEvent('domready', function(){
milkbox.restoreOptions();
});
Milkbox now has 2 custom events: onXmlGalleries and onClosed.
- Use onXmlGalleries when you want to know if an XML file is completely loaded. See the XML Galleries paragraph for details.
- Use onClosed when you want to do something immediately after Milkbox is closed. I personally use this to restore the originally setted options or css if for some reason I temporarily changed them:
window.addEvent('domready',function(){
$('testbutton').addEvent('click',function(){
milkbox.changeOptions({ resizeTransition:'bounce:out', canvasBorderWidth:'5px' });
milkbox.addEvent('closed',function(){
milkbox.restoreOptions();
});
milkbox.showGallery({ gallery:'gall1' })
})
});
You can set it globally via the autoPlay and autoPlayDelay options like described in the 'Global JavaScript options' paragraph.
If you use an XML gallery, you can set different autoplay options for every gallery.
Finally, you can override the options on a per-gallery basis by using these methods (described below):
setAutoPlay() showGallery()
But what's autoPlay? Well, yes, it's that! ;)
Milkbox will sequentially show all the files in a gallery, with a pre-defined delay between each.
You will be able to stop it either by clicking the play/pause button or the next/prev buttons. You will then use the play/pause button to re-start it.
You use setAutoPlay() if you want to permanently override the global autoplay options for one or more galleries.
You can pass it an object or an array of objects. Every object should have 3 properties: gallery (string), autoplay (boolean), delay (number).
window.addEvent('domready', function(){
milkbox.setAutoPlay([
{ gallery:'gall1', autoplay:true, delay:10 },
{ gallery:'gall2', autoplay:false },
{ gallery:'gall3', autoplay:true, delay:7 }
]);
});
Instead of clicking a link, you can launch Milkbox directly from JavaScript (and therefore even from Flash, via ExternalInterface), usign the showGallery() method.
You use showGallery() if you want to launch Milkbox directly from JavaScript. You can launch any gallery, starting from any file and with custom autoplay options. Autoplay options here are not modified permanetly: the originals will be restored after you close Milkbox. To permanently change autoPlay options, use setAutoPlay().
You can pass showGallery() an object. That object must have a gallery property (string: the name of the gallery you setted in the rel property), and optionally 3 more properties: index:(number), autoplay (boolean), delay (number).
window.addEvent('domready', function(){
$('testbutton').addEvent('click', function(e){
e.preventDefault();
milkbox.showGallery({ gallery:'mygallery', index:3, autoplay:true, delay:10 })
});
});
If you want to lauch Milkbox from flash, simply use ExternalInterface:
import flash.external.ExternalInterface;
//if you need, load some xml:
ExternalInterface.call("milkbox.addGalleries",'galleries.xml');
//AS2: ////////////////////////////////////
sampleButton.onRelease = function(){
ExternalInterface.call("milkbox.showGallery",{gallery:'mygallery', index:3, autoplay:true});
}
//or AS3: /////////////////////////////////
function openMilkboxFromAS(e:MouseEvent){
ExternalInterface.call("milkbox.showGallery",{gallery:'mygallery', index:3, autoplay:true});
}
sampleButton.addEventListener(MouseEvent.CLICK,openMilkboxFromAS);
Here there's a really nice tutorial written by Rafael Dery explaining how to build a flash gallery with Milkbox.
If you don't need to write HTML links, but still want to use Milkbox to show some gallery, you need to define them in some other way. Enter the XML galleries. :)
First of all, you need to write an xml file, for example:
<?xml version="1.0" encoding="utf-8"?> <div id="galleries"> <div class="gallery" name="xmlG1" autoplay="true" delay="7" > <a href="swf/testswf1.swf" rev="width:550, height:400" title="Lorem ipsum..." /> <a href="swf/testswf2.swf" rev="width:650, height:250" title="Lorem ipsum..." /> <a href="img/01b.jpg" title="Lorem ipsum..." /> <a href="img/03b.jpg" title="Lorem <a href=#>ipsum...</a>" /> </div> <div class="gallery" name="xmlG2" > <a href="img/01b.jpg" title="Lorem ipsum..." /> <a href="img/03b.jpg" title="Lorem <a href=#>ipsum...</a>" /> <a href="swf/testswf1.swf" rev="width:550, height:400" title="Lorem ipsum..." /> <a href="swf/testswf2.swf" rev="width:650, height:250" title="Lorem ipsum..." /> </div> </div>
As you can see, for every gallery you must use div tags with class="gallery" and set the name property. Optionally, you can add custom autoplay options for every gallery.
Please note that, since it's an xml document, tags are NOT real html tags, and the link tags must be empty tags, so you close them with a space followed by a slash ((an empty space)/>), not with a close tag like this: </a>
Once you have your xml file, load it with the addGalleries() method:
milkbox.addGalleries('galleries.xml');
Now you can use all the Milkbox methods with the new galleries. I think you'll at least use showGallery(), but before doing it, it's safer to be sure that the xml file is really loaded, so you wrap it in the onXmlGalleries custom event:
milkbox.addEvent('xmlGalleries',function(){
$('testButton').addEvent('click', function(e){
e.preventDefault();
milkbox.showGallery({ gallery:'xmlG2' });
});
});
milkbox.addGalleries('galleries.xml');
Often, using Ajax, links and galleries change without fully refreshing the main page and therefore without updating the internal Milkbox galleries.
To solve these kind of issues, you can simply use the reloadGalleries() method. For example, a generic Ajax application could do something like this:
app.ajaxRequest.onSuccess(response){
addResponseToHtml();
milkbox.reloadGalleries();
}
A little note: using Milkbox in a page that contains flash objects, you must set their wmode property to 'opaque' or 'transparent'.
If you use Milkbox, I'D REALLY LIKE YOU TO DONATE some little money with paypal:
Note: after donating, you will receive an automatic e-mail notification by PayPal. So if you donate as a company, you should be able to use that notification for tax records.