- Overview
- Documents
Fotorama is a highly flexible image gallery plugin for jQuery that works in both desktop and mobile browsers. It offers multiple options for browsing through the images including thumbnails, prev-next buttons, swiping, slideshow or bullet navigation.
Source: fotorama.io
1. INCLUDE JS AND CSS FILES
<!-- 1. Link to jQuery (1.8 or later), --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <!-- 33 KB --> <!-- fotorama.css & fotorama.js. --> <link href="http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.5.1/fotorama.css" rel="stylesheet"> <!-- 3 KB --> <script src="http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.5.1/fotorama.js"></script> <!-- 16 KB -->
2. HTML
<!-- 2. Add images to <div class="fotorama"></div>. --> <div class="fotorama"> <img src="http://s.fotorama.io/1.jpg"> <img src="http://s.fotorama.io/2.jpg"> </div>
3. JAVASCRIPT
$('.fotorama').fotorama({ width: 700, maxwidth: '100%', ratio: 16/9, allowfullscreen: true, nav: 'thumbs' });
4. OPTIONS
width Number or String
Stage container width in pixels or percents.
Stage container minimum width in pixels or percents.
Stage container maximum width in pixels or percents.
Stage container height in pixels or percents.
Stage container minimum height in pixels or percents.
Stage container maximum height in pixels or percents.
Width divided by height. Recommended if you’re using percentage width.
Horizontal margins for frames in pixels.
Glimpse size of nearby frames in pixels or percents.
Navigation style:
-
'dots' Default
iPhone-style dots. -
'thumbs'
Thumbnails. -
false
Nothing.
Navigation block position relative to stage: 'bottom' or 'top'.
Tumbnail width in pixels.
Tumbnail height in pixels.
Size of thumbnail margins.
Border width of the active thumbnail.
allowfullscreen Boolean or String
Allows fullscreen:
-
false Default
-
true
-
'native'
How to fit an image into a fotorama:
-
'contain' Default
-
'cover'
-
'scaledown'
-
'none'
Defines what transition to use:
-
'slide' Default
-
'crossfade'
-
'dissolve'
Animation length in milliseconds.
Captions visibility.
Index or id of the frame that will be shown upon initialization of the fotorama.
Enables loop.
Enables slideshow. Turn it on with true or any interval in milliseconds.
Stops slideshow at any user action with the fotorama.
Enables keyboard navigation.
Turns on navigation arrows over the frames.
Moving between frames by clicking.
Moving between frames by swiping.
Enables trackpad support and horizontal mouse wheel as well.
Shuffles frames at launch.
Sets the frames direction: ltr or rtl.
Spinner options:
$('.fotorama').fotorama({ spinner: { lines: 13, color: 'rgba(0, 0, 0, .75)' } });
Enables shadows.
5. API
Variables
Zero-based position of the fotorama within the all fotoramas on the page.
Number of frames in the fotorama.
Zero-based position of the active frame.
Frame data object with following key properties:
-
img String
Image URL. -
thumb String
Thumbnail URL. -
full String
Fullscreen image URL. -
id String
Given id. -
$stageFrame jQuery
DOM element of the active stage frame. Wrapped in a jQuery. -
$navDotFrame jQuery
DOM element of the active navigation frame with iPhone-style dot. Wrapped in a jQuery. -
$navThumbFrame jQuery
DOM element of the active navigation frame with thumbnail. Wrapped in a jQuery.
Methods
Shows the frame with the set index, zero-based number:
// Go to the first frame: fotorama.show(0); // Fourth: fotorama.show(3);
...or keystring:
// Next: fotorama.show('>'); // Previous: fotorama.show('<'); // Last: fotorama.show('>>'); // Arbitrary id: fotorama.show('some-id');
Shows the frame with the set index and in the given time:
fotorama.show({ index: 1, // The second frame. time: 500 // Half-second transition. });
fotorama.startAutoplay([interval])
Starts the slideshow with a set interval in milliseconds. Default iterval is 5000, 5 seconds.
Stops the slideshow.
Opens fotorama in fullscreen mode
Leaves the fullscreen mode.
Starts the video if the active frame has it.
Stops and unloads the currently playing video.
Changes the size of the fotorama, options is an object with width, minwidth, maxwidth, height,minheight, maxheight, and ratio that you can pass to force a certain size:
fotorama.resize({ width: 500, height: 333 });
Animate the changes by passing the time:
fotorama.resize({ width: 333, height: 500, time: 1000 });
Manipulates the fotorama options at runtime:
fotorama.setOptions({ nav: false, arrows: false });
Loads new data into the fotorama. It should be structured as an array and follow the same patterns as the original data:
fotorama.load([ {img: '1.jpg', thumb: '1-thumb.jpg'}, {img: '2.jpg', thumb: '2-thumb.jpg'} ]);
fotorama.push(frame1, ..., frameN)
Appends the given frames to the fotorama:
fotorama.push({img: '3.jpg', thumb: '3-thumb.jpg'});
Removes the last frame.
Removes the first frame.
fotorama.unshift(frame1, ..., frameN)
Adds one or more frames to the beginning of the fotorama.
fotorama.splice(index , howMany[, frame1, ..., frameN])
Changes the data array, adding new frames while removing old frames. It works just like the Array.splice:
// Remove two frames after the first: fotorama.splice(0, 2); // Replace the last frame: fotorama.splice(-1, 1, {img: 'new-last.jpg'});
Reverses the fotorama in place.
fotorama.sort([compareFunction])
Sorts the frames like the Array.sort:
items.sort(function (frameA, frameB) { if (frameA.img > frameA.img) return 1; if (frameB.img < frameB.img) return -1; return 0; });
Shuffles the frames.
Kills the fotorama and restores the original content.
6. EVENTS
Attach callbacks to the Fotorama events. Every callback contains the event object and refference to the API as a function arguments:
$('.fotorama').on('fotorama:ready', function (e, fotorama) { console.log(e.type, fotorama.activeIndex); });
Some of the handlers contain third extra argument with additional data:
$('.fotorama').on( 'fotorama:show fotorama:showend', function (e, fotorama, extra) { console.log(e.type + (extra.user ? ' after user’s touch' : '')); console.log('transition duration: ' + extra.time); } );
$('.fotorama').on('fotorama:load', function (e, fotorama, extra) { console.log(extra.src + ' is loaded'); });
It is recommended to start listening to the events before initialization. Here is the snippet to explore all the Fotorama events in console:
<script> $(function () { $('.fotorama') // Listen to the events .on('fotorama:ready ' + // Fotorama is fully ready 'fotorama:show ' + // Start of transition to the new frame 'fotorama:showend ' + // End of the show transition 'fotorama:load ' + // Stage image of some frame is loaded 'fotorama:error ' + // Stage image of some frame is broken 'fotorama:startautoplay ' + // Slideshow is started 'fotorama:stopautoplay ' + // Slideshow is stopped 'fotorama:fullscreenenter ' + // Fotorama is fullscreened 'fotorama:fullscreenexit ' + // Fotorama is unfullscreened 'fotorama:loadvideo ' + // Video iframe is loaded 'fotorama:unloadvideo', // Video iframe is removed function (e, fotorama, extra) { console.log('## ' + e.type); console.log('active frame', fotorama.activeFrame); console.log('additional data', extra); } ) // Initialize fotorama manually .fotorama(); }); </script> <div class="fotorama" data-auto="false"> <!-- ↑ Important attribute. --> <img src="1.jpg"> <img src="2.jpg"> </div>