瀑布流

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            .box{
                width: 200px;
                background-color: aqua;
                font-size: 100px;
                text-align: center;
                color: white;
                position: absolute;
            }
        </style>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        //数组用来保存每一列的总高度
        var heightArray=[0,0,0]
        for(var i=0;i<40;i++){
            var newDiv=document.createElement('div');
            newDiv.className='box';
            var num =Math.random()*100+50;
            newDiv.style.height=num+"px";
            
            
            //1、找出高度最小的那一列下标
            var minIndex=0;
            for(var j=1;j<heightArray.length;j++){
                if(heightArray[minIndex]>heightArray[j]){
                    minIndex=j;
                }
            }
            //2、设置Div的left=列下标*(200+10)
            newDiv.style.left=minIndex*210+"px";
            //3、设置div的top=最小列的高度+10;
            newDiv.style.top=heightArray[minIndex]+"px";
            //4、把Div的高度加到最小列
            newDiv.innerHTML=i;
            heightArray[minIndex]+=(num+10);
            

            document.body.appendChild(newDiv);
        }
        
    </script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容