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 at 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="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.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.0.4" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/jquery.fancybox.pack.js?v=2.0.4"></script> <!-- Optionaly add button and/or thumbnail helpers --> <link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-buttons.css?v=2.0.4" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-buttons.js?v=2.0.4"></script> <link rel="stylesheet" href="/fancybox/source/helpers/jquery.fancybox-thumbs.css?v=2.0.4" type="text/css" media="screen" /> <script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-thumbs.js?v=2.0.4"></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>
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.
fancyBox will try to guess content type from href attribute but you can specify it directly by adding classname (fancybox.image, fancybox.iframe, etc).
Example: <a class="fancybox.iframe" href="http://jquery.com">Open iframe</a>
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.
You can pass these options as key/value object to fancybox() method. It is alos possible to modify defaults directly at fancyBox JS file or $.fancybox.defaults
Name | Description |
---|---|
padding | Space inside fancyBox around content |
margin | Minimum space between viewport and fancyBox. Can be set as array - [top, right, bottom, left]. Right and bottom margins are ignored if content dimensions exceeds viewport |
width | Default width for 'iframe' and 'swf' content. Also for 'inline', 'ajax' and 'html' if 'autoSize' is set to 'false' |
height | Default height for 'iframe' and 'swf' content. Also for 'inline', 'ajax' and 'html' if 'autoSize' is set to 'false' |
minWidth | Minimum width fancyBox should be allowed to resize to |
minHeight | Minimum height fancyBox should be allowed to resize to |
maxWidth | Maximum width fancyBox should be allowed to resize to |
maxHeight | Maximum height fancyBox should be allowed to resize to |
autoSize | If set to true, for 'inline', 'ajax' and 'html' type content width/height is auto determined. If no dimensions set this may give unexpected results |
fitToView | If set to true, fancyBox is resized to fit inside viewport |
aspectRatio | If set to true, resizing is constrained by the original aspect ratio (images always keep ratio) |
topRatio | Top space ratio for vertical centering. If set to 0.5 than vertical and bottom spece will be equal. If 0 - fancyBox will be at the viewport top |
fixed | If set to true, fancyBox position will be changed to 'fixed' if it fits inside viewport |
scrolling | Set the overflow CSS property to create or hide scrollbars. Can be set to 'auto', 'yes' or 'no' |
wrapCSS | Customizable CSS class for wrapping element (great for custom styling) |
arrows | If set to true, navigation arrows will be displayed |
closeBtn | If set to true, close button will be displayed |
closeClick | If set to true, fancyBox will be closed when user clicks the content |
nextClick | If set to true, will navigate to next gallery item when user clicks the content |
mouseWheel | If set to true, you will be able to navigate gallery using the mouse wheel |
loop | If set to true, enables cyclic navigation. This means, if you click "next" after you reach the last element, first element will be displayed (and vice versa). |
ajax | Options for ajax request |
keys | Define keyboard keys for gallery navigation and closing |
modal | If set to true, will disable navigation and closing |
autoPlay | If set to true, slideshow will start after opening the first gallery item |
playSpeed | Slideshow speed in milliseconds |
index | Overrides group start index |
type | Overrides type for content |
href | Overrides content source link |
content | Overrides content to be displayed |
title | Overrides title |
tpl | Object containing various templates |
openEffect / closeEffect / nextEffect / prevEffect | Animation effect ('elastic', 'fade' or 'none') for each transition type |
openSpeed / closeSpeed / nextSpeed / prevSpeed | The time it takes (in ms, or "slow", "normal", "fast") to complete transition |
openEasing / closeEasing / nextEasing / prevEasing | Easing method for each transition type. You have numerous choices if easing plugin is included |
openOpacity / closeOpacity | If set to true, transparency is changed for elastic transitions |
openMethod / closeMethod / nextMethod / prevMethod | Method from $.fancybox.transitions() that handles transition (you can add custom effects there) |
helpers | Object containing options for enabled helpers |
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.