flexImages

A lightweight jQuery plugin for creating fluid galleries as seen on Flickr and Google Images.

Overview and Features

Live Demo

Usage

Include the stylesheet jquery.flex-images.css in the <head> section of your HTML document - and the JavsScript file jquery.flex-images.min.js after loading jQuery. flexImages accepts settings from an object of key/value pairs.

$(selector).flexImages({key1: value1, key2: value2});

To update any settings or to reload modified content, simply re-initialize flexImages again on the appropriate container. The plugin automatically prevents multiple bindings to one and the same element. This may be used for content that gets modified later on via AJAX.

Settings

Property Default Description
container '.item' Selector of the individual image/object containers.
object 'img' Selector of the image/object inside a container.
rowHeight 180 Maximum height of a row.
maxRows null Maximum number of rows to display. Images/Objects exceeding this row are hidden.
truncate false Hide incomplete last row of images/objects.

Example Code

HTML Markup for the on page live demo

<div class="flex-images">
    <div class="item" data-w="200" data-h="150"><img src="http://lorempixel.com/200/150/?1"></div>
    <div class="item" data-w="250" data-h="150"><img src="http://lorempixel.com/250/150/?2"></div>
    <div class="item" data-w="150" data-h="150"><img src="http://lorempixel.com/150/150/?3"></div>
    <div class="item" data-w="150" data-h="150"><img src="http://lorempixel.com/150/150/?4"></div>
    <div class="item" data-w="200" data-h="150"><img src="http://lorempixel.com/200/150/?5"></div>
    <div class="item" data-w="100" data-h="150"><img src="http://lorempixel.com/100/150/?6"></div>
    <div class="item" data-w="180" data-h="150"><img src="http://lorempixel.com/180/150/?7"></div>
    <div class="item" data-w="185" data-h="150"><img src="http://lorempixel.com/185/150/?8"></div>
    <div class="item" data-w="350" data-h="150"><img src="http://lorempixel.com/350/150/?9"></div>
    <div class="item" data-w="200" data-h="150"><img src="http://lorempixel.com/200/150/?10"></div>
    <div class="item" data-w="165" data-h="150"><img src="http://lorempixel.com/165/150/?11"></div>
    <div class="item" data-w="150" data-h="150"><img src="http://lorempixel.com/150/150/?12"></div>
    <div class="item" data-w="120" data-h="150"><img src="http://lorempixel.com/120/150/?13"></div>
    <div class="item" data-w="120" data-h="150"><img src="http://lorempixel.com/120/150/?14"></div>
    <div class="item" data-w="140" data-h="150"><img src="http://lorempixel.com/140/150/?15"></div>
    <div class="item" data-w="200" data-h="150"><img src="http://lorempixel.com/200/150/?16"></div>
    <div class="item" data-w="170" data-h="150"><img src="http://lorempixel.com/170/150/?17"></div>
</div>

Each image must be wrapped inside a container element. The container must have a width (data-w) and height (data-h) attribute set that corresponds to the image's/object's original dimension.

JavaScript code

$('.flex-images').flexImages({rowHeight: 140});

How it works: The plugin dynamically changes the width and height of all containers. The images themselves have their height and width set to 100%. Thus, each image takes up the full space inside its container.

This architecture enables a very flexible grid layout that works with more than just images. E.g. objects may be placed on top of the images via absolute positioning. Or instead of images, the containers may be filled with videos or even plain text.

More Examples

Please take a look at the source code of this page to see how these demos work.

1. Fixed number of two complete rows with lazy loading of images; only images that are actually visible are retrieved from the server:

For lazy loading of images or iframes, it's required to use a "blank.gif" pixel as the source of all images. The actual source URL must be given in a data-src attribute of the image. On init, the plugin will replace the dummy src attribute (blank.gif) with the actual source URL for all visible images.

2. A fluid grid with video content and only full rows:

3. Showing a title underneath + less margin between images:

Caption 1
Caption 2
Caption 3
Caption 4
Caption 5

4. Overlaying caption - could be a link or a button, as well:

Caption 1
Caption 2
Caption 3
Caption 4
Caption 5

5. Cut off parts of the images, e.g. a Shutterstock ID:

Original (uncut) first image with Shutterstock ID at the bottom: