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

image.png

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

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模型 81楼

新建文件夹 (4).rar


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

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #tabBox{
            background-color: #f1f1f1;
            border: 1px solid #ff0000;
        }

        .active{
            background-color: #e9f212;
        }

        #tabBox div{
            display: none;
            width: 300px;
            height: 250px;
            padding: 10px;
            background-color: skyblue;
            border: green
        }
    </style>
    <script>
        window.onload = function(){
            var tabBox = document.getElementById('tabBox');
            var tabBtn = tabBox.getElementsByTagName('input');
            var tabDiv = tabBox.getElementsByTagName('div');

            for(var i=0;i<tabBtn.length;i++){
                tabBtn[i].index = i;
                tanBtn[i].onclick = function(){
                    for(var j=0;j<tabBtn.length;j++){
                        tabBtn[j].className = ''
                        tabDiv[j].style.display = 'none';
                    }
                    this.className = 'active';
                    tabDiv[this.index].style.display = 'block';
                }
            }
        }
    </script>
</head>

<body>

    <div id="tabBox">
        <input type="button" value="web" class="active">
        <input type="button" value="Java">
        <input type="button" value="Python">
        <div style="display: block">React, Vue</div>
        <div>SpringBoot, SpringMVC</div>
        <div>flask</div>
    </div>

    <script>
       

    </script>
</body>

</html>

切换不了呀!!!

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 88楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 89楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 90楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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