|
11 | 11 | options = $.extend( {}, $.fn.rowGrid.defaults, options );
|
12 | 12 | $this.data('grid-options', options);
|
13 | 13 | layout(this, options);
|
14 |
| - |
| 14 | + |
15 | 15 | if(options.resize) {
|
16 | 16 | $(window).on('resize.rowGrid', {container: this}, function(event) {
|
17 | 17 | layout(event.data.container, options);
|
|
20 | 20 | }
|
21 | 21 | });
|
22 | 22 | };
|
23 |
| - |
| 23 | + |
24 | 24 | $.fn.rowGrid.defaults = {
|
25 | 25 | minMargin: null,
|
26 | 26 | maxMargin: null,
|
27 | 27 | resize: true,
|
28 | 28 | lastRowClass: 'last-row',
|
29 | 29 | firstItemClass: null
|
30 | 30 | };
|
31 |
| - |
| 31 | + |
32 | 32 | function layout(container, options, items) {
|
33 | 33 | var rowWidth = 0,
|
34 | 34 | rowElems = [],
|
35 | 35 | items = jQuery.makeArray(items || container.querySelectorAll(options.itemSelector)),
|
36 | 36 | itemsSize = items.length;
|
37 | 37 | // 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')); |
39 | 41 | var itemAttrs = [];
|
40 | 42 | var theImage, w, h;
|
41 | 43 | for(var i = 0; i < itemsSize; ++i) {
|
|
49 | 51 | // get width and height via attribute or js value
|
50 | 52 | if (!(w = parseInt(theImage.getAttribute('width')))) {
|
51 | 53 | theImage.setAttribute('width', w = theImage.offsetWidth);
|
52 |
| - } |
| 54 | + } |
53 | 55 | if (!(h = parseInt(theImage.getAttribute('height')))) {
|
54 | 56 | theImage.setAttribute('height', h = theImage.offsetHeight);
|
55 |
| - } |
56 |
| - |
| 57 | + } |
| 58 | + |
57 | 59 | itemAttrs[i] = {
|
58 | 60 | width: w,
|
59 | 61 | height: h
|
|
73 | 75 |
|
74 | 76 | rowWidth += itemAttrs[index].width;
|
75 | 77 | rowElems.push(items[index]);
|
76 |
| - |
| 78 | + |
77 | 79 | // check if it is the last element
|
78 | 80 | if(index === itemsSize - 1) {
|
79 | 81 | for(var rowElemIndex = 0; rowElemIndex<rowElems.length; rowElemIndex++) {
|
|
86 | 88 | 'height: ' + itemAttrs[index+parseInt(rowElemIndex)-rowElems.length+1].height + 'px;' +
|
87 | 89 | 'margin-right:' + ((rowElemIndex < rowElems.length - 1)?options.minMargin+'px' : 0);
|
88 | 90 | }
|
89 |
| - } |
90 |
| - |
| 91 | + } |
| 92 | + |
91 | 93 | // check whether width of row is too high
|
92 | 94 | if(rowWidth + options.maxMargin * (rowElems.length - 1) > containerWidth) {
|
93 | 95 | var diff = rowWidth + options.maxMargin * (rowElems.length - 1) - containerWidth;
|
|
0 commit comments