会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题

/* 
    瀑布流效果
        1、第二行的第一张图片,应该放在每列的高度最矮的图片下面
 */
// 1、动态设置页面居中
window.onload = function(){
    waterFlow();
}


function waterFlow(){
    // 获取父级容器
    var parentContainer = document.getElementById("container");
    // 获取子元素
    var allChild = document.getElementsByClassName("box");
    // 获取屏幕
    var screenWidth = document.documentElement.clientWidth;
    // 获取一个图片容器的宽度
    var childWidth = allChild[0].offsetWidth;
    // 计算屏幕水平方向最多能摆放的图片个数
    var rowsNum = Math.floor(screenWidth / childWidth) - 1; // 向下取整,预留一张作为空白区
    // 让页面左右居中
    parentContainer.style.cssText = "width:" + rowsNum * childWidth + "px;margin:0 auto;";

    getMinHeightOfCols(allChild,rowsNum);
}

// 2、动态设置图片位置

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

            //高度的合并
            colsHeightArr[minHeightOfIndex]=colsHeightArr[minHeightOfIndex]+allChild[i].offsetHeight;
        }
    }

}

image.pngimage.png

找了好几遍,不知道为什么,总是有那个空隙?

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 136楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 138楼

老师,对于document.body.clientWidth/document.body.clientHeight

document.body.offsetWidth/document.body.offsetHeight

document.body.scrollWidth/document.body.scrollHeight

document.body.scrollTop/document.body.scrollLeft

这几个有点不太明白,我理解是offsetWidth=clientWidth+border;可是在变网页窗口的时候,offsetWidth比clientWidth小。不知道为什么我这么写之后,网页就不显示div了。

f5b1aa9a5fa1d7bc4d2e63a7ca19485.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    </style>
<body style="height: 1000px">
<div style="background: #0d3349;width: 300px;height: 2000px"></div>
</body>
    <script type="text/javascript">
        window.onload=function(){
            document.write("网页可见区域宽:"+document.body.clientWidth+"</br>");
            document.write("网页可见区域高client:"+document.body.clientHeight+"</br>");
            document.write("网页可见区域宽:"+document.body.offsetWidth+"</br>");
            document.write("网页可见区域高offset:"+document.body.offsetHeight+"</br>");
            document.write("网页正文全文宽:"+document.body.scrollWidth);
            document.write("网页正文全文高:"+document.body.scrollHeight);
            document.write("网页被卷去的高:"+document.body.scrollTop);
            document.write("网页被卷去的左:"+document.body.scrollLeft);
            document.write("网页正文部分上:"+window.screenTop);
            document.write("网页正文部分左:"+window.screenLeft);
            document.write("网页正文部分上:"+window.screenX);
            document.write("网页正文部分左:"+window.screenY);
            document.write("屏幕分辨率的高:"+window.screen.height);
            document.write("屏幕分辨率的宽:"+window.screen.width);
            document.write("屏幕可用工作区高度:"+window.screen.availHeight);
            document.write("屏幕可用工作区宽度:"+window.screen.availWidth);
            document.write("浏览器高度:"+window.outerHeight+"</br>");
            document.write("浏览器宽度:"+window.outerWidth+"</br>");
            document.write("浏览器内页面可用高度:"+window.innerHeight+"</br>");
            document.write("浏览器内页面可用宽度:"+window.innerWidth);
        }
    </script>
</head>
</html>


WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 144楼
WEB前端全系列/第二阶段:JavaScript编程模块/字符串与数组 145楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 147楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 148楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 149楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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