div混合 三元运算符

mumubangditu mumubangditu 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是一个 div混合的 代码,现在的代码显示200*20, 100*10, 100*20,100*10(空白)四种形式的DIV,随机显示在一个1000*200的DIV里。

如何修改三元运算符,添加一个300*10的形式?使得100*10的显示20个,100*20的显示15个,200*20的显示10个,300*10的显示10个,300*20的显示5个。(上面的背景色随意设置,主要为了区分不同的DIV)

剩余的均显示100*10的背景色白色(30个),谢谢。

<style>
*,p{padding:0;margin:0;}
</style>
<?php
function divHtml($dtype, $x, $y)
{ $ww =($dtype ==3) ?'200px':'100px'; $hh =($dtype==2)?'40px':'20px'; $bgcols =array('#999', '#ccc', '#eee');
$xx =($x*100) .'px'; $yy =($y*20) .'px'; $bgc =$bgcols[$dtype-1];
return "<p style='position:absolute; width:$ww;height:$hh; left:$xx;top:$yy; background:$bgc;'> $dtype </p>";
}
$divs =array();
$cells =array();
for ($i=0; $i<60; $i++) $cells[$i] =0;

function reserve($dtype, $x, $y)
{ global $cells, $divs; if ($y*10+$x >59 ||($cells[$y*10+$x])) return false;
switch ($dtype)
{ case 2: if ($y ==5 || $cells[$y*10+$x] ||$cells[($y+1)*10+$x]) return false; $cells[($y+1)*10+$x] =1; break;
case 3: if ($x ==9 || $cells[$y*10+$x] ||$cells[$y*10+$x+1]) return false; $cells[$y*10+$x+1] =1; break;
}
$cells[$y*10+$x] =1; $divs[] =divHtml($dtype, $x, $y); return true;
}

for($i = 0; $i < 10; $i ++) while (!reserve(2, rand(0,9), rand(0,5))) ;
for($i = 0; $i < 10; $i ++) while (!reserve(3, rand(0,9), rand(0,5))) ;
for($i = 0; $i < 60; $i ++) if (!$cells[$i]) reserve(1, $i%10, floor($i/10)) ;

echo '<div style="position:absolute; width:1000px;height:200px;">';
foreach ($divs as $single) echo $single;
echo '</div>';
?>
...全文

相关推荐

评论 12
收藏 已收藏
分享
返回顶部
mumubangditu
基础编程 21,889 成员 140,329 内容