I have a grid, that has been design with fixed width and height. See image below.
This grid, must work on all screen sizes, while keeping the items in the grid in the same positions.
My code currently resizes these items based off the original width/height of the design and fits them within the new width/height.
Although they do not get positioned correctly in the example below, the calculation does work.
The difficulty I have, is that I need the centre grid tile, which is 504x504, to be static and the others to adjust with this taken into account.
Currently all items, including the centre one adjust, I just want to stop the centre one from adjusting and the others to remove/add the difference to their width/height.
This is a tricky on to explain, let me know if I can add any more details.
const initalWidth = 1043;
const initialHeight = 708;
const resize = (baseDimension, newDimension, itemDimension) => {
const diff = ((baseDimension - newDimension) / baseDimension) * 100;
return (Math.ceil(diff) / 100) * itemDimension;
}
const update = () => {
const newWidth = jQuery(document).width();
const newHeight = jQuery(document).height();
jQuery('.grid').css({
width: newWidth,
height: newHeight
})
jQuery('.grid-item').each((i, item) => {
const itemWidth = jQuery(item).data('width');
const itemHeight = jQuery(item).data('height');
const widthDiff = resize(initalWidth, newWidth, itemWidth);
const heightDiff = resize(initialHeight, newHeight, itemHeight);
jQuery(item).width(Math.floor((itemWidth - widthDiff)));
jQuery(item).height(Math.floor((itemHeight - heightDiff)));
})
}
jQuery(document).ready(() => {
// Run once
update()
// Run on resize
jQuery(window).resize(update);
});
.grid{
border: 1px solid blue;
width: 100%;
height: 100%;
}
.grid-item{
background-color: red;
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="grid">
<div class="grid-item" data-width="257" data-height="249" style="background-color: red;"></div>
<div class="grid-item" data-width="502" data-height="97" style="background-color: blue;"></div>
<div class="grid-item" data-width="504" data-height="504" style="background-color: yellow;"></div>
<div class="grid-item" data-width="275" data-height="254" style="background-color: brown;"></div>
<div class="grid-item" data-width="275" data-height="254" style="background-color: pink;"></div>
<div class="grid-item" data-width="275" data-height="198" style="background-color: orange;"></div>
<div class="grid-item" data-width="275" data-height="247" style="background-color: purple;"></div>
<div class="grid-item" data-width="502" data-height="97" style="background-color: green;"></div>
</div>