Цель состоит в том, чтобы создать гибкий макет, как показано ниже.
Пока у меня есть рабочая функция moveBox (lastBox, "east")
, которая отслеживает индексы строк и столбцов.
function moveBox(box, where) {
switch (where) {
case "north":
lastTopOffset -= BOX_HEIGHT + BOX_MARGIN;
box.style.top = lastTopOffset + 'px';
box.style.left = lastLeftOffset + 'px';
rowIndex -= 1;
break;
// ...
}
Мой текущий код ,
(function () {
var i, lastBox,
MAX_DIVS = 72,
BOX_HEIGHT = 50,
BOX_WIDTH = 100,
BOX_MARGIN = 5,
field = document.getElementById('fieldPerimeter'),
fieldHeight = field.offsetHeight,
maxRows = Math.floor(fieldHeight / (BOX_HEIGHT + BOX_MARGIN)),
rowIndex = 0,
colIndex = 0,
lastLeftOffset = 0,
lastTopOffset = 0;
function moveBox(box, where) {
switch (where) {
case "north":
lastTopOffset -= BOX_HEIGHT + BOX_MARGIN;
box.style.top = lastTopOffset + 'px';
box.style.left = lastLeftOffset + 'px';
rowIndex -= 1;
break;
case "east":
lastLeftOffset += BOX_WIDTH + BOX_MARGIN;
box.style.top = lastTopOffset + 'px';
box.style.left = lastLeftOffset + 'px';
colIndex += 1;
break;
case "south":
lastTopOffset += BOX_HEIGHT + BOX_MARGIN;
box.style.top = lastTopOffset + 'px';
box.style.left = lastLeftOffset + 'px';
rowIndex += 1;
break;
default:
break;
}
}
for (i = 0; i < MAX_DIVS; i += 1) {
lastBox = document.createElement('div');
lastBox.className = 'box';
lastBox.innerHTML = i;
field.appendChild(lastBox);
//delete me
if( (i + 1) % 2 === 0 || (i + 1)% 3 === 0){
moveBox(lastBox, "east");
} else {
moveBox(lastBox, "south");
}
//delete me
// if(rowIndex < maxRows && rowIndex > 0){
// if (colIndex % 4 === 0){
// moveBox(lastBox, "south");
// } else if (colIndex % 2 === 0){
// moveBox(lastBox, "north");
// } else {
// moveBox(lastBox, "east");
// }
// }
}
})();
добавляет элементы div в контейнер, а затем перемещает его. В приведенном ниже коде показана часть моих попыток указать, когда двигаться на север или юг. Но я не могу добиться желаемого макета.
if (colIndex % 4 === 0) { moveBox(lastBox, "south"); }
else if (colIndex % 2 === 0) { moveBox(lastBox, "north"); }
else { moveBox(lastBox, "east"); }