Skip to content

Commit b6a5fe7

Browse files
Fix for fractional container width rounding
element.clientWidth rounds the width value (see https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth). If the width is rounded up, then rowGrid.js overestimates how much space it has and the last image can wrap to its own row. This fix uses getBoundingClientRect() to get the true width of the container. The width property of the DOMRect object wasn’t included until IE9, so for compatibility with IE8 this fix computers the width using the right and left properties.
1 parent be4a5ed commit b6a5fe7

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Diff for: jquery.row-grid.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
options = $.extend( {}, $.fn.rowGrid.defaults, options );
1212
$this.data('grid-options', options);
1313
layout(this, options);
14-
14+
1515
if(options.resize) {
1616
$(window).on('resize.rowGrid', {container: this}, function(event) {
1717
layout(event.data.container, options);
@@ -20,22 +20,24 @@
2020
}
2121
});
2222
};
23-
23+
2424
$.fn.rowGrid.defaults = {
2525
minMargin: null,
2626
maxMargin: null,
2727
resize: true,
2828
lastRowClass: 'last-row',
2929
firstItemClass: null
3030
};
31-
31+
3232
function layout(container, options, items) {
3333
var rowWidth = 0,
3434
rowElems = [],
3535
items = jQuery.makeArray(items || container.querySelectorAll(options.itemSelector)),
3636
itemsSize = items.length;
3737
// read
38-
var containerWidth = container.clientWidth-parseFloat($(container).css('padding-left'))-parseFloat($(container).css('padding-right'));
38+
39+
var containerBoundingRect = container.getBoundingClientRect();
40+
var containerWidth = Math.floor(containerBoundingRect.right - containerBoundingRect.left)-parseFloat($(container).css('padding-left'))-parseFloat($(container).css('padding-right'));
3941
var itemAttrs = [];
4042
var theImage, w, h;
4143
for(var i = 0; i < itemsSize; ++i) {
@@ -49,11 +51,11 @@
4951
// get width and height via attribute or js value
5052
if (!(w = parseInt(theImage.getAttribute('width')))) {
5153
theImage.setAttribute('width', w = theImage.offsetWidth);
52-
}
54+
}
5355
if (!(h = parseInt(theImage.getAttribute('height')))) {
5456
theImage.setAttribute('height', h = theImage.offsetHeight);
55-
}
56-
57+
}
58+
5759
itemAttrs[i] = {
5860
width: w,
5961
height: h
@@ -73,7 +75,7 @@
7375

7476
rowWidth += itemAttrs[index].width;
7577
rowElems.push(items[index]);
76-
78+
7779
// check if it is the last element
7880
if(index === itemsSize - 1) {
7981
for(var rowElemIndex = 0; rowElemIndex<rowElems.length; rowElemIndex++) {
@@ -86,8 +88,8 @@
8688
'height: ' + itemAttrs[index+parseInt(rowElemIndex)-rowElems.length+1].height + 'px;' +
8789
'margin-right:' + ((rowElemIndex < rowElems.length - 1)?options.minMargin+'px' : 0);
8890
}
89-
}
90-
91+
}
92+
9193
// check whether width of row is too high
9294
if(rowWidth + options.maxMargin * (rowElems.length - 1) > containerWidth) {
9395
var diff = rowWidth + options.maxMargin * (rowElems.length - 1) - containerWidth;

0 commit comments

Comments
 (0)