会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 76楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 77楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 78楼
WEB前端全系列/第二阶段:JavaScript编程模块/字符串与数组 80楼
WEB前端全系列/第二阶段:JavaScript编程模块/算法与数据 81楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 82楼

image.png

WEB前端全系列/第二阶段:JavaScript编程模块/函数与对象 84楼

function waterFlow(){
    var parentconter = document.getElementById("conter");  
    var allChild = document.getElementById("conter").getElementsByClassName("boximg");
    var screenWidth = document.documentElement.clientWidth;
    var childWidth = allChild[0].offsetWidth;
    var rowsNum = Math.floor(screenWidth/childWidth)-1;
    parentconter.style.cssText="width:"+rowsNum*childWidth+"px;margin: 0 auto;"

    getMinHeightOfCols(allChild,rowsNum)
}
waterFlow()

// 动态设置图片位置
function getMinHeightOfCols(allChild,rowsNum){
    // 存储每列高度
    var colsHeightArr = [];
    for(var i =0;i<allChild.length;i++){
        //判断取出第一行的图片,获取图片对应高度,放入到数组中 
        if(i<rowsNum){
           //获取当前图片高度
           colsHeightArr[i] = allChild[i].offsetHeight;
        //    console.log(colsHeightArr[i])
        }else{
            //获取高度最小的值
            var minHeightofCols = Math.min.apply(null,colsHeightArr);
            // console.log(minHeightofCols)
            //获取最小值对应的下标(位置)
            var minHeightOfIndex = colsHeightArr.indexOf(minHeightofCols);
            console.iog(minHeightOfIndex)
            //摆放第二列图片位置
            allChild[i].style.position = "absolute";
            allChild[i].style.top=minHeightOfCols+"px";
            allChild[i].style.left=allChild[minHeightOfIndex].offsetleft+"px";
            colsHeightArr[minHeightOfIndex] = colsHeightArr[minHeightOfIndex] + allChild[i].offsetHeight;
        }
    }
}

下标位置获取不到,无法添加定位样式

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 85楼

新建文件夹 (4).rar


控制台打印的摆放图片数量,页面宽度和实际不一样

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 86楼
WEB前端全系列/第二阶段:JavaScript编程模块/算法与数据 88楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 90楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637