v字形

转载 (原文地址) xiaodading 随笔 js初级 666阅读 2014-08-21 17:16:43 举报

[code]<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div{width:50px;height: 50px;background: red;position: absolute;left:0;top: 0;text-align: center;line-height: 50px;}
</style>
</head>
<body>

</body>
<script>
/var str='';
for(var i=0; i<11; i++){
str+='<div>'+i+'</div>';
}
document.body.innerHTML=str;
var aDiv=document.getElementsByTagName('div');
for(var i=0; i<11; i++){
aDiv[i].style.left = 10+i
60+'px';
if(i<=5){
aDiv[i].style.top = 10+i60+'px';
}else{
aDiv[i].style.top = 10+(11-i-1)
60+'px';
}
}*/

//改进
var len=21;
var cen=Math.floor(len/2);
var str='';
for(var i=0; i<len; i++){
    str+='<div>'+i+'</div>';
}
document.body.innerHTML=str;
var aDiv=document.getElementsByTagName('div');
for(var i=0; i<len; i++){
    aDiv[i].style.left = 10+i*60+'px';
    if(i<=cen){
        aDiv[i].style.top = 10+i*60+'px';
    }else{
        aDiv[i].style.top = 10+(len-i-1)*60+'px';
    }
}

</script>
</html>[/code]代码片段 1

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复