日期:2014-05-16 浏览次数:20409 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>自动换列</title> <style type="text/css"> body{padding:0px; margin:0px;} </style> <script type="text/javascript"> function viewList(){ //这个你根据需要自己获取节点的数组 var arr = document.getElementsByTagName('div');//获取所有要显示的图标 var iconNum = arr.length;//获取多少个显示的图标 var screen_h = document.documentElement.clientHeight;//页面的高度 var colNum = Math.floor(screen_h/50);//每列显示多少个 var col = Math.ceil((iconNum*50)/screen_h);//总共能显示多少列 document.title = "个数:"+iconNum+",高度:"+screen_h+",列数:"+col; for(var i=0;i<iconNum;i++){ var x = (Math.floor(i/colNum))*50; var y = (i%colNum)*50; arr[i].style.top = y+"px"; arr[i].style.left = x+"px"; } } </script> </head> <body onload="viewList()" onresize="viewList()"> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">1</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">2</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">3</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">4</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">5</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">6</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">7</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">8</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">9</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">10</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">11</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">12</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">13</div> <div style="height:48px;width:48px;border:1px #0F6 solid;position:absolute;">14</div> </body> </html>