I am trying to write a module that automatically subdivides div-elements, given some initial parameters.
I've prepared the following jsfiddle.
What I am doing at the moment is:
- Setting the width and height of the parent element.
- Setting the child elements to float left and assigning percentual width and height to them, based on the specified parameters.
that.createDivisionData = function(paramObj) {
var
numOfDivisions = paramObj.divisions,
widthTop = paramObj.widthTop,
heightTop = paramObj.heightTop,
widthBottom = paramObj.widthBottom,
widthBlock = paramObj.blockWidth,
heightBlock = paramObj.blockHeight,
data = [];
for ( var i = 1; i <= numOfDivisions; i++) {
var divisionObj = {};
if (i < 3) {
divisionObj.widthPerc = i % 2 ? widthTop + '%' : (100 - widthTop) + '%';
divisionObj.width = i % 2 ? widthTop * widthBlock / 100 : (100 - widthTop) * widthBlock / 100;
divisionObj.heightPerc = heightTop + '%';
divisionObj.height = heightBlock * heightTop / 100;
} else {
divisionObj.widthPerc = i % 2 ? widthBottom + '%' : (100 - widthBottom) + '%';
divisionObj.width = i % 2 ? widthBottom * widthBlock / 100 : (100 - widthBottom) * widthBlock / 100;
divisionObj.heightPerc = (100 - heightTop) + '%';
divisionObj.height = (100 - heightTop) * heightBlock / 100;
}
data.push(divisionObj);
}
return data;
};
The way I am doing it at the moment actually works. But it feels somehow wrong, dirty, unfinished, you name it. I know this is a subjective question, but maybe someone has an idea. How can I do this in a cleaner way?
if (i < 3) { ...
What is different about those first two items versus the rest? \$\endgroup\$