I tried to use PHP to make DIV random positions. we can look each 100px as a unit, type one small div is 1*1 ,type two small div is 1*2, type three small div is 2*1. All divs only allow show in a big div box 10*6. Here is my code.
<?php
function DIV1()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:100px;background:#ff0 none;\"></div>";
}
function DIV2()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:200px;background:#f00 none;\"></div>";
}
function DIV3()
{
$choose1 = array("0","100","200","300","400","500","600","700","800");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:200px;height:100px;background:#00f none;\"></div>";
}
echo '<div style="width:1000px;height:600px;">';
$sizes = array();
for($i = 0; $i < 15; $i ++) $sizes[] = DIV1($row);
for($i = 0; $i < 10; $i ++) $sizes[] = DIV2($row);
for($i = 0; $i < 5; $i ++) $sizes[] = DIV3($row);
shuffle($sizes);
for($i = 0; $i < 30; $i ++) echo $sizes[$i];
echo '</div>';
?>
I still have some css questions. in my code, I make postion:absolute and top left setting, but some divs will overlapping. how to solve it? Thanks.
1) You should use “return” instead of “echo” in the end line of 3 div functions so that array sizes can have any effect
2) Your problem is not standard, it is a little bit mathematical challenge, you must do “remembering” of positions and shuffling by hand
3) can this below code be your solution ? ( test here )