Skip to content

Image box with corrected to be based on image width #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions flex-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (c) 2014 Simon Steinberger / Pixabay
GitHub: https://github.com/Pixabay/JavaScript-flexImages
License: http://www.opensource.org/licenses/mit-license.php
*/
*/

var flexImages = (function(){
// "use strict";
Expand All @@ -29,7 +29,9 @@ var flexImages = (function(){
row_width += items[i][2] + o.margin;
if (row_width >= max_w) {
var margins_in_row = row.length * o.margin;
ratio = (max_w-margins_in_row) / (row_width-margins_in_row), row_h = Math.ceil(o.rowHeight*ratio), exact_w = 0, new_w;
var ratio = (max_w-margins_in_row) / (row_width-margins_in_row);
var row_h = Math.ceil(o.rowHeight*ratio);
var exact_w = 0, new_w;
for (x=0; x<row.length; x++) {
new_w = Math.ceil(row[x][2]*ratio);
exact_w += new_w + o.margin;
Expand Down Expand Up @@ -57,14 +59,19 @@ var flexImages = (function(){

for (var i=0;i<grids.length;i++) {
var grid = grids[i], containers = grid.querySelectorAll(o.container), items = [], t = new Date().getTime();
if (options['rowHeight'] == 'fluid'){
grid.clientWidth > 900 ? o['rowHeight'] = Math.floor(grid.clientWidth / 5) : o['rowHeight'] = 180;
}
if (!containers.length) continue;
var s = window.getComputedStyle ? getComputedStyle(containers[0], null) : containers[0].currentStyle;
o.margin = (parseInt(s.marginLeft) || 0) + (parseInt(s.marginRight) || 0) + (Math.round(parseFloat(s.borderLeftWidth)) || 0) + (Math.round(parseFloat(s.borderRightWidth)) || 0);
for (var j=0;j<containers.length;j++) {
var c = containers[j],
w = parseInt(c.getAttribute('data-w')),
norm_w = w*(o.rowHeight/parseInt(c.getAttribute('data-h'))), // normalized width
obj = c.querySelector(o.object);
w = parseInt(c.getAttribute('data-w')),
obj = c.querySelector(o.object),
objWidth = obj.width,
objHeight = obj.height,
norm_w = objWidth*(o.rowHeight/objHeight);// normalized width
items.push([c, w, norm_w, obj, obj.getAttribute('data-src')]);
}
makeGrid(grid, items, o);
Expand All @@ -90,3 +97,4 @@ var flexImages = (function(){
else
window.flexImages = flexImages;
})();