Skip to content

Instantly share code, notes, and snippets.

@PavelGonzales
Created April 28, 2018 14:11
Show Gist options
  • Save PavelGonzales/05c20ce7b4b2c4f153714875f0b527e4 to your computer and use it in GitHub Desktop.
Save PavelGonzales/05c20ce7b4b2c4f153714875f0b527e4 to your computer and use it in GitHub Desktop.
/**
* Метод принимает
* @param {array} row // массив картинок в ряду.
* @param {HTMLElement} wrap // родитель картинок, наш компонент.
**/
function flow(row, wrap) {
const rowArray = Array.from(row);
let aspect = 0;
const horizontalPadding = 4;
const maxHeight = 500;
const length = row.length;
// Собираем все картинки высчитываем соотношение сторон.
const imagesArray = rowArray.map(image => {
const imageWidth = image.naturalWidth;
const imageHeight = image.naturalHeight;
const imageAspect = imageWidth / imageHeight || 1;
aspect = aspect + imageAspect;
return {image, aspect: imageAspect};
});
// Высчитываем ширину ряда что-бы вычислить процентную ширину каждой картинки.
const rowWidth = wrap.offsetWidth;
// Высчитываем высоту ряда.
let rowHeight = (rowWidth - horizontalPadding * length) / aspect;
if ((rowHeight > maxHeight) && this.totalImages === 1) {
rowHeight = maxHeight;
}
// Устанавливаем ширину каждой картинке в ряду.
imagesArray.forEach(image => {
image.image.parentElement.style.width = `${(rowHeight * image.aspect) * 100 / rowWidth}%`;
});
return imagesArray;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment