fancyBox is a tool that offers a nice and elegant way to add zooming functionality for images, html content and multi-media on your webpages. It is built on the top of the popular JavaScript framework jQuery and is both easy to implement and a snap to customize.
Version 2 is completely rewritten with new features and updated graphics.
Download the plugin, unzip it, copy files and include fancyBox script and stylesheet in your document (you will need to make sure the css and js files are on your server, and adjust the paths in the script and link tag). Make sure you also load the jQuery library. Example:
<!-- Add jQuery library --> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <!-- Add mousewheel plugin (this is optional) --> <script type="text/javascript" src="/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script> <!-- Add fancyBox --> <link rel="stylesheet" href="/fancybox/source/jquery.fancybox.css?v=2.1.5" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script> <!-- Optionally add helpers - button, thumbnail and/or media --> <link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-buttons.css?v=1.0.5" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-buttons.js?v=1.0.5"></script> <script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.6"></script> <link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=1.0.7" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=1.0.7"></script>
Create link elements whose href attributes will contain the path of the element you wish to open within the fancyBox.
<a class="fancybox" rel="group" href="big_image_1.jpg"><img src="small_image_1.jpg" alt="" /></a> <a class="fancybox" rel="group" href="big_image_2.jpg"><img src="small_image_2.jpg" alt="" /></a>
Attach fancyBox when the document is loaded. If you are not familiar with jQuery, please, read this tutorial for beginners.
<script type="text/javascript"> $(document).ready(function() { $(".fancybox").fancybox(); }); </script>
A collection of the demos created in response to questions
You can also go straight into action and open a standalone version of demo and view its source code.
Note: ID's are single use and are only applied to one element.
Galleries are created from elements who have the same "data-fancybox-group" or "rel" attribute value.
Script uses the `href` or `data-fancybox-href` attribute of the
matched elements to obtain the location of the content and to figure out
content type you want to display.
You can specify type directly by adding classname (fancybox.image,
fancybox.iframe, etc) or `data-fancybox-type` attribute.
Use `title` or `data-fancybox-title` attribute to specify item
caption.
<ul class="list"> <li> <a class="various fancybox.ajax" href="/demo/ajax.php">Ajax</a> </li> <li> <a class="various" data-fancybox-type="iframe" href="/demo/iframe.html">Iframe</a> </li> <li> <a class="various" href="#inline">Inline</a> </li> <li> <a class="various" href="http://www.adobe.com/jp/events/cs3_web_edition_tour/swfs/perform.swf">SWF</a> </li> </ul> <ul class="list"> <li> <a class="various fancybox.iframe" href="http://www.youtube.com/embed/L9szn1QQfas?autoplay=1">Youtube (iframe)</a> </li> <li> <a class="various fancybox.iframe" href="http://maps.google.com/?output=embed&f=q&source=s_q&hl=en&geocode=&q=London+Eye,+County+Hall,+Westminster+Bridge+Road,+London,+United+Kingdom&hl=lv&ll=51.504155,-0.117749&spn=0.00571,0.016512&sll=56.879635,24.603189&sspn=10.280244,33.815918&vpsrc=6&hq=London+Eye&radius=15000&t=h&z=17">Google maps (iframe)</a> </li> <li> <a class="various" href="/data/non_existing_image.jpg">Non-existing url</a> </li> </ul>
Alternatively, you can set content type as an option: $(".open_ajax").fancybox({type: 'ajax'});
.
Note, ajax requests are subject to the same origin policy. If fancyBox will not be able to get content type, it will try to guess based on 'href' and will quit silently if would not succeed (this is different from previous versions where 'ajax' was used as default type or an error message was displayed).
Remember to include the necessary files! Each helper is located in separate files.
fancyBox licensed under Creative Commons Attribution-NonCommercial 3.0 license.
You are free to use fancyBox for your personal or non-profit website projects.
You can get the author's permission to use fancyBox for commercial websites by paying a fee.
The latest source code is available on GitHub.
The plugin comes with a number of public functions to help you utilize the plugin in a number of different scenarios.
Each of these functions can be called as a property of the $.fancybox
object.
To use from inside the iframe - <a href="javascript:parent.$.fancybox.close();">Close me</a>
Name | Description |
---|---|
open |
$.fancybox([group], [options]) First parameter can be in various types, examples: $.fancybox([
{href : 'img1.jpg', title : 'Title'},
{href : 'img2.jpg', title : 'Title'}
]); - array containing objects $.fancybox( {href : 'image.jpg', title : 'Lorem lipsum'} ); - single object $.fancybox( ['image.jpg', 'image.jpg'] ); - array containing links as strings$.fancybox( 'image.jpg' ); - string as source link $.fancybox( [jQuery object] ); $.fancybox( '<h1>Lorem lipsum</h1>' );
|
cancel |
|
close |
$.fancybox.close( true ) ) than fancyBox is removed immediatly
|
play |
|
next |
|
prev |
|
jumpto |
$.fancybox.jumpto( 0 ); will open the first, e.g. $.fancybox.jumpto( 1 ); will second, etc
|
reposition |
|
update |
|
toggle |
|
showLoading |
|
hideLoading |
|
Helpers provide a simple mechanism to extend the capabilities of fancyBox. There are two built-in helpers - 'overlay' and 'title'. You can disable them, set custom options or enable other helpers. Examples:
//Disable title helper $(".fancybox").fancybox({ helpers: { title: null } }); //Disable overlay helper $(".fancybox").fancybox({ helpers: { overlay : null } }); //Change title position; show overlay after content has loaded $(".fancybox").fancybox({ helpers: { title : { type : 'inside' }, overlay : { showEarly : false } } }); //Enable thumbnail helper and set custom options $(".fancybox").fancybox({ helpers: { thumbs : { width: 50, height: 50 } } });
Name | Description |
---|---|
width | Thumbnail width |
height | Thumbnail height |
source |
Function to obtain the URL of the thumbnail image. By default, it
uses the first image inside anchor or loads destination url instead. Examples: http://jsfiddle.net/PFVxK/, http://jsfiddle.net/2k8EP/ |
position | 'top' or 'bottom' |
Name | Description |
---|---|
tpl | HTML template |
position | 'top' or 'bottom' |
You can ask a questions using the StackOverflow site where you are most likely to get answer quickly as many Javascript experts spend time on the site. Make sure you add the tags "jquery" and "fancybox" when posting.
If you run into an issue and need to report a bug, please create an issue on GitHub issues and I will investigate.
Follow @thefancyapps for the latest updates.