A simple jQuery plugin to arrange images into a flexible grid, based on Tumblr's photoset feature. Originally the plugin was created for our Style Hatch Tumblr themes as a way to use the photoset grid in responsive layouts, but we have since expanded it for use outside of the themes.
48 | 49 |Demos & Usage
50 | 51 | 52 |Basic Photoset Grid
53 | 58 |Simply call photosetGrid();
on a div with the data-layout
specified and a number of images inside.
HTML:
61 | 62 |<div class="photoset-grid-basic" data-layout="12">
63 | <img src="img/demo/nyc1-500px.jpg" width="1280" height="960" data-highres="img/demo/nyc1-highres.jpg">
64 | <img src="img/demo/nyc2-500px.jpg" width="500" height="375" data-highres="img/demo/nyc2-highres.jpg">
65 | <img src="img/demo/nyc3-500px.jpg" width="500" height="667" data-highres="img/demo/nyc3-highres.jpg">
66 | </div>
67 |
68 | Javascript:
69 | 70 |$('.photoset-grid-basic').photosetGrid();
71 |
72 |
73 | 74 | 75 | 76 |
Custom Options
77 | 82 | 83 |Beyond the basic usage, you can set a number of optional arguments including callback functions that are useful for adding a lightbox for high resolution images. Additionally the images in this example do not specify height and width, so the plugin waits for all the images to load before laying out the grid.
84 |HTML:
85 | 86 |<div class="photoset-grid-custom" style="visibility: hidden;">
87 | <img src="img/demo/print1-500px.jpg" data-highres="img/demo/print1-highres.jpg">
88 | <img src="img/demo/print2-500px.jpg" data-highres="img/demo/print2-highres.jpg">
89 | <img src="img/demo/print3-500px.jpg" data-highres="img/demo/print3-highres.jpg">
90 | </div>
91 |
92 | Javascript:
93 | 94 |$('.photoset-grid-custom').photosetGrid({
95 | // Set the gutter between columns and rows
96 | gutter: '5px',
97 | // Manually set the grid layout
98 | layout: '21',
99 | // Wrap the images in links
100 | highresLinks: true,
101 | // Asign a common rel attribute
102 | rel: 'print-gallery',
103 | // Add a border to each image
104 | borderActive: true,
105 | borderWidth: '3px',
106 | borderColor: '#000000',
107 | borderRadius: '3px',
108 | borderRemoveDouble: false,
109 |
110 | onInit: function(){},
111 | onComplete: function(){
112 | // Show the grid after it renders
113 | $('.photoset-grid-custom').attr('style', '');
114 | }
115 | });
116 |
117 |
118 | 119 | 120 | 121 |
Adding A Lightbox
122 | 129 | 130 |This demonstration of the photoset grid uses the onComplete
event to assign a lightbox plugin to view the high resolution images. The code below is specific to jquery.colorbox.js, but it should work virtually the same for other plugins.
HTML:
133 | 134 |<div class="photoset-grid-lightbox" data-layout="131" style="visibility: hidden;">
135 | <img src="img/demo/withhearts1-500px.jpg" width="1280" height="1707" data-highres="img/demo/withhearts1-highres.jpg">
136 | <img src="img/demo/withhearts2-500px.jpg" width="500" height="663" data-highres="img/demo/withhearts2-highres.jpg">
137 | <img src="img/demo/withhearts3-500px.jpg" width="500" height="500" data-highres="img/demo/withhearts3-highres.jpg">
138 | <img src="img/demo/withhearts4-500px.jpg" width="500" height="500" data-highres="img/demo/withhearts4-highres.jpg">
139 | <img src="img/demo/withhearts5-500px.jpg" width="1280" height="1280" data-highres="img/demo/withhearts5-highres.jpg">
140 | </div>
141 |
142 | Javascript:
143 | 144 |$('.photoset-grid-lightbox').photosetGrid({
145 | highresLinks: true,
146 | rel: 'withhearts-gallery',
147 | gutter: '2px',
148 |
149 | onComplete: function(){
150 | $('.photoset-grid-lightbox').attr('style', '');
151 | $('.photoset-grid-lightbox a').colorbox({
152 | photo: true,
153 | scalePhotos: true,
154 | maxHeight:'90%',
155 | maxWidth:'90%'
156 | });
157 | }
158 | });
159 |
160 |
161 |
162 | 163 | 164 | 165 |
Adding Photoset Grid to Tumblr Themes
166 | 167 |HTML:
168 | 169 |{block:Photoset}
170 | <div class="photoset-grid" data-layout="{PhotosetLayout}" data-id="photoset{PostID}">
171 | {block:Photos}
172 | <img src="{PhotoURL-500}"
173 | {block:HighRes}data-highres="{PhotoURL-HighRes}"{/block:HighRes}
174 | width="{PhotoWidth-500}" height="{PhotoHeight-500}"
175 | {block:Caption}alt="{Caption}"{/block:caption} />
176 | {/block:Photos}
177 | </div>
178 |
179 | {block:Caption}
180 | {Caption}
181 | {/block:caption}
182 | {/block:Photoset}
183 |
184 | Javascript:
185 | 186 |$('.photoset-grid').photosetGrid({
187 | highresLinks: true,
188 | rel: $('.photoset-grid').attr('data-id'),
189 | gutter: '5px',
190 |
191 | onComplete: function(){}
192 | });
193 |
194 |
195 |
196 | Usage
197 |Apply the photo set grid layout to a selected div
containing images for the grid.
The only markup requirement is a data-layout
attribute on the selected div
. data-layout
should contain a string of numbers representing the number of columns for each row.
As an option you can set the height
and width
attributes on all the images to instantly layout the grid, otherwise the plugin will wait for all images to load.
Understanding data-layout:
203 | 204 |-
205 |
data-layout="2331"
1st row has 2 images, 2nd row has 3 images, 3rd row has 3 images, and 4th row has 1 image. Total of 9 images.
206 | data-layout="13"
1st row has 1 image and 2nd row has 3 images.
207 |
Arguments:
210 | 211 |-
212 |
width
-string
Change the width that the photo set grid will be rendered at. Default:100%
automatically fits its container for responsive layouts
213 | layout
-string
Manually set a string of numbers to specify the number of images each row contains. Default:null
generates a stacked layout of one image per row
214 | gutter
-string
Set the pixel width between the columns and rows. Default:0px
215 | highresLinks
-boolean
Set totrue
to automatically swap out the default imagesrc
with thedata-highres
attribute once the image is wider thanlowresWidth
. This will also wrap each image with ana
vs.div
element. Default:false
216 | lowresWidth
-number
Sets the width where the default image is swapped out for the high resolution image. Default:500
217 | rel
-string
This optional setting useful for lightbox viewers applies a common `rel` attribute to the anchor tags wrapping the images. Default:''
218 | borderActive
-boolean
This optional setting is used to wrap each image with a border. Default:false
219 | borderWidth
-string
Define the width of the border wrapping each image. Default:'5px'
220 | borderColor
-string
Defines the color used for the border wrapping each image. Default:'#000000'
221 | borderRadius
-string
Defines the border radius of the border wrapping each image. Default:'0'
222 | borderRemoveDouble
-boolean
If the gutter is set to 0px (or not specified) then this option can be used to remove double borders that would occur between each row/cell. Default:false
223 | onInit
-function
Define a function to be called when the plugin is initialized.
224 | onComplete
-function
Define a function to be called when the plugin has completed the grid layout.
225 |
Installation
228 | 229 |Bower package manager
230 | 231 |You can easily install photoset-grid as a Bower package by running:
232 | 233 |$ bower install photoset-grid
234 |
235 | Credits
236 | 237 |-
238 |
- Jonathan Moore - @moore | jonathanmoore.com 239 |
- Mikey Wills - @mukealicious | muke.me 240 | 241 |
- Additional
242 |
-
243 |
- Thomas Sardyha & David DeSandro - desandro/imagesloaded 244 |
- Louis-Rémi Babé - louisremi/jquery-smartresize 245 |
247 |