首页 > 代码库 > for计算元素坐标

for计算元素坐标

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>

<style>
div { width:50px; height:50px; background:red; position:absolute; top:0; left:0; font-size:30px; text-align:center; line-height:50px; color:#fff; }
</style>

<script>
window.onload = function (){
var aDiv = document.getElementsByTagName(‘div‘);

for( var i=0; i<11; i++ ){
document.body.innerHTML += ‘<div>‘ + i + ‘</div>‘;
}

for( var i=0; i<aDiv.length; i++ ){
aDiv[i].style.left = 10 + i*50 + ‘px‘;
aDiv[i].style.top = 10 + i*50 + ‘px‘;
}

// 逢10 往下一行(小练习)
// 来一个小V字形(小练习)

};
</script>
</head>

<body>

 

</body>
</html>

for计算元素坐标