El objetivo es producir un diseño fluido como se muestra a continuación.Algoritmo de diseño de fluido similar a la serpiente
Hasta ahora no tienen una función de trabajo moveBox(lastBox, "east")
que realiza un seguimiento de los índices de fila y columna.
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;
// ...
}
Mi current code,
(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");
// }
// }
}
})();
anexa divs a un contenedor y luego lo mueve. El siguiente código muestra parte de mis intentos de especificar cuándo moverse hacia el norte o hacia el sur. Pero estoy luchando para lograr el diseño deseado.
if (colIndex % 4 === 0) { moveBox(lastBox, "south"); }
else if (colIndex % 2 === 0) { moveBox(lastBox, "north"); }
else { moveBox(lastBox, "east"); }
un violín, la demostración de la imagen, y el código (gracias a Bart Kiers) en una pregunta interesante de un nuevo usuario? Upvote. Ojalá todos los nuevos usuarios tuvieran esto a su alcance. ':)' –
¿Es la forma en que lo ha abordado un requisito o simplemente el que ha intentado? –
He intentado otros enfoques, lo he publicado porque creo que estoy cerca, pero mi cabeza explota tratando de descubrir el algoritmo. Más tarde quiero usar un valor aleatorio para no ir al norte o al sur, pero por ahora solo necesito un poco de guía con algún pseudocódigo. Gracias Sr. Kiers por la edición. – user1032739