php动态每3个div后设置不同的属性

时间:2014-04-14 07:17:35

标签: php css css3

我想在每3个div之后设置css TOP属性不同

例如:

<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
<div style="top:100px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
<div style="top:150px;">
</div>
**Now after this the next 3 div's shall have**
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
<div style="top:200px;">
</div>
**and so on**

我想用php动态地做这个。 使用for循环我可以检查是否

$ij=0; if($ij%3==0) {
    echo "";
    }
    else {
    }

但不确定如何获得理想的结果。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

试试这个:

$top = 50;
$n = 30; // no. of divs
for($i = 0; $i < $n; $i++) {
    if($i % 3==0) $top+=50;
    echo "<div style=\"top:".$top."px;\">\n</div>";
}