Skip to content

Commit 9750421

Browse files
committed
improve rowGrid.js
1 parent 3ce8ffc commit 9750421

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# rowGrid.js
2-
**rowGrid.js is a small, lightweight (~800 bytes gzipped) jQuery plugin for placing images (or other items) in straight rows.**
2+
**rowGrid.js is a small, lightweight (~900 bytes gzipped) jQuery plugin for placing images (or other items) in straight rows.**
33

44
The grid is similar to grids on Google Image Search, flickr, shutterstock and Google+ images.
55

66
Features:
77

88
* responisve
99
* infinite scrolling
10+
* support for all modern browsers and IE >= 9
1011

1112
[![Example Grid](http://brunjo.github.io/rowGrid.js/example.png)][2]
1213

@@ -17,7 +18,8 @@ All items must have the **same height** but the **width can be variable**. RowGr
1718
At first rowGrid.js adjusts the margin between the items. If this is not enough rowGrid.js scales down the items.
1819

1920
## Demos & Examples
20-
http://brunjo.github.io/rowGrid.js/
21+
Examples with explanation: http://brunjo.github.io/rowGrid.js/
22+
Real world example: http://www.pexels.com/
2123

2224
## Installation
2325
RowGrid.js requires jQuery 1.7 or above.

example/index.html

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ <h1><a href="https://github.com/brunjo/rowGrid.js">rowGrid.js Example</a></h1>
8686
<img src="http://lorempixel.com/220/200?14" width="220" height="200" />
8787
</div>
8888
<div class="item">
89-
<img src="http://lorempixel.com/210/240?15" width="240" height="200" />
89+
<img src="http://lorempixel.com/240/200?15" width="240" height="200" />
9090
</div>
9191
<div class="item">
92-
<img src="http://lorempixel.com/220/180?16" width="180" height="200" />
92+
<img src="http://lorempixel.com/180/200?16" width="180" height="200" />
9393
</div>
9494
<div class="item">
95-
<img src="http://lorempixel.com/220/200?17" width="200" height="200" />
95+
<img src="http://lorempixel.com/200/200?17" width="200" height="200" />
9696
</div>
9797
<div class="item">
9898
<img src="http://lorempixel.com/210/200?18" width="210" height="200" />
@@ -152,13 +152,13 @@ <h1><a href="https://github.com/brunjo/rowGrid.js">rowGrid.js Example</a></h1>
152152
<img src="http://lorempixel.com/220/200?14" width="220" height="200" />
153153
</div>
154154
<div class="item">
155-
<img src="http://lorempixel.com/210/240?15" width="240" height="200" />
155+
<img src="http://lorempixel.com/240/200?15" width="240" height="200" />
156156
</div>
157157
<div class="item">
158-
<img src="http://lorempixel.com/220/180?16" width="180" height="200" />
158+
<img src="http://lorempixel.com/180/200?16" width="180" height="200" />
159159
</div>
160160
<div class="item">
161-
<img src="http://lorempixel.com/220/200?17" width="200" height="200" />
161+
<img src="http://lorempixel.com/200/200?17" width="200" height="200" />
162162
</div>
163163
<div class="item">
164164
<img src="http://lorempixel.com/210/200?18" width="210" height="200" />

jquery.row-grid.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
if(options === 'appended') {
66
options = $this.data('grid-options');
77
var $lastRow = $this.children('.' + options.lastRowClass);
8-
var items = $lastRow.nextAll().add($lastRow);
8+
var items = $lastRow.nextAll(options.itemSelector).add($lastRow);
99
layout(this, options, items);
1010
} else {
1111
options = $.extend( {}, $.fn.rowGrid.defaults, options );
@@ -32,44 +32,49 @@
3232
function layout(container, options, items) {
3333
var rowWidth = 0,
3434
rowElems = [],
35-
items = items || container.querySelectorAll(options.itemSelector),
35+
items = Array.prototype.slice.call(items || container.querySelectorAll(options.itemSelector)),
3636
itemsSize = items.length;
37+
// read
38+
var containerWidth = container.clientWidth-parseFloat($(container).css('padding-left'))-parseFloat($(container).css('padding-right'));
39+
var itemAttrs = [];
40+
var theImage;
41+
for(var i = 0; i < itemsSize; ++i) {
42+
theImage = items[i].getElementsByTagName('img')[0];
43+
if (!theImage) {
44+
items.splice(i, 1);
45+
--i;
46+
--itemsSize;
47+
continue;
48+
}
49+
// get width and height via attribute or js value
50+
itemAttrs[i] = {
51+
width: parseInt(theImage.getAttribute('width')) || theImage.offsetWidth,
52+
height: parseInt(theImage.getAttribute('height')) || theImage.offsetHeight
53+
};
54+
}
55+
itemsSize = items.length;
3756

57+
// write
3858
for(var index = 0; index < itemsSize; ++index) {
39-
items[index].removeAttribute('style');
4059
if (items[index].classList) {
4160
items[index].classList.remove(options.firstItemClass, options.lastRowClass);
4261
}
4362
else {
4463
// IE <10
4564
items[index].className = items[index].className.replace(new RegExp('(^|\\b)' + options.firstItemClass + '|' + options.lastRowClass + '(\\b|$)', 'gi'), ' ');
4665
}
47-
}
4866

49-
// read
50-
// only IE >8
51-
var containerWidth = parseInt(getComputedStyle(container).width);
52-
var itemAttrs = [];
53-
for(var i = 0; i < itemsSize; ++i) {
54-
itemAttrs[i] = {
55-
outerWidth: items[i].offsetWidth,
56-
height: items[i].offsetHeight
57-
};
58-
}
59-
60-
// write
61-
for(var index = 0; index < itemsSize; ++index) {
62-
rowWidth += itemAttrs[index].outerWidth;
67+
rowWidth += itemAttrs[index].width;
6368
rowElems.push(items[index]);
6469

6570
// check if it is the last element
6671
if(index === itemsSize - 1) {
6772
for(var rowElemIndex = 0; rowElemIndex<rowElems.length; rowElemIndex++) {
68-
// if first element in row
73+
// if first element in row
6974
if(rowElemIndex === 0) {
7075
rowElems[rowElemIndex].className += ' ' + options.lastRowClass;
7176
}
72-
rowElems[rowElemIndex].style.marginRight = (rowElemIndex < rowElems.length - 1)?options.minMargin+'px' : 0;
77+
rowElems[rowElemIndex].style.cssText = 'margin-right:' + ((rowElemIndex < rowElems.length - 1)?options.minMargin+'px' : 0);
7378
}
7479
}
7580

@@ -90,7 +95,7 @@
9095
widthDiff = 0;
9196
for(var rowElemIndex = 0; rowElemIndex<rowElems.length; rowElemIndex++) {
9297
rowElem = rowElems[rowElemIndex];
93-
var rowElemWidth = itemAttrs[index+parseInt(rowElemIndex)-rowElems.length+1].outerWidth;
98+
var rowElemWidth = itemAttrs[index+parseInt(rowElemIndex)-rowElems.length+1].width;
9499
var newWidth = rowElemWidth - (rowElemWidth / rowWidth) * diff;
95100
var newHeight = Math.round(itemAttrs[index+parseInt(rowElemIndex)-rowElems.length+1].height * (newWidth / rowElemWidth));
96101
if (widthDiff + 1 - newWidth % 1 >= 0.5 ) {

jquery.row-grid.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)