zoukankan      html  css  js  c++  java
  • 瀑布流

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style>
    * {
    margin: 0;
    padding: 0;
    }

    .box {
    float: left;
    border: 1px solid #ccc;
    padding: 5px;
    }
    </style>
    <script>
    //第一行图片不用计算 自然摆放即可
    //后面的要通过JS去摆放
    //找到高度最短的这一列去追加
    window.onload=function(){
    //计算列数
    var container=document.getElementById("container");
    var boxes=container.children;
    var pageWidth=window.innerWidth;
    var boxWidth=boxes[0].offsetWidth;
    var column=Math.floor(pageWidth/boxWidth);
    console.log(column);

    //找最小列
    var arrHeight=[];
    function waterfall(){
    for (var i=0;i<boxes.length;i++){
    if (i<column){
    arrHeight[i]=boxes[i].offsetHeight;
    }else {
    var minHeight=getMin(arrHeight).value;
    var minHeightIndex=getMin(arrHeight).index;
    boxes[i].style.position="absolute";
    boxes[i].style.left=boxes[minHeightIndex].offsetLeft+"px";
    boxes[i].style.top=minHeight+"px";
    arrHeight[minHeightIndex]=minHeight+boxes[i].offsetHeight;
    }
    }
    }
    waterfall();
    var minHeight=getMin(arrHeight).value;
    var minHeightIndex=getMin(arrHeight).index;
    console.log(arrHeight);
    console.log(minHeight);
    console.log(minHeightIndex);

    //判断触底
    window.onscroll=function(){
    if (bottomed()){

    var json = [
    {"src": "images/P_000.jpg"},
    {"src": "images/P_001.jpg"},
    {"src": "images/P_002.jpg"},
    {"src": "images/P_003.jpg"},
    {"src": "images/P_004.jpg"},
    {"src": "images/P_005.jpg"},
    {"src": "images/P_006.jpg"},
    {"src": "images/P_007.jpg"},
    {"src": "images/P_008.jpg"},
    {"src": "images/P_009.jpg"}
    ]
    for(var i=0;i<json.length;i++){
    var div=document.createElement("div");
    div.className="box";
    container.appendChild(div);
    var img=document.createElement("img");
    img.src=json[i].src;
    div.appendChild(img);
    }
    waterfall();
    }

    }
    function bottomed(){
    var clientHeight=window.innerHeight;
    var scrollTop=window.pageYOffset;
    var lastBoxTop=boxes[boxes.length-1].offsetTop;
    if (clientHeight+scrollTop>lastBoxTop){
    return true;
    }
    }
    }
    function getMin(arr){
    var min={};
    min.index=0;
    min.value=arr[min.index];
    for (var i=0;i<arr.length;i++){
    if (arr[i]<min.value){
    min.value=arr[i];
    min.index=i;
    }
    }
    return min;
    }
    </script>
    </head>
    <body>
    <div id="container">
    <!--(.box>img[src="images/P_00$.jpg"])*9-->
    <div class="box"><img src="images/P_000.jpg" alt=""/></div>
    <div class="box"><img src="images/P_001.jpg" alt=""/></div>
    <div class="box"><img src="images/P_002.jpg" alt=""/></div>
    <div class="box"><img src="images/P_003.jpg" alt=""/></div>
    <div class="box"><img src="images/P_004.jpg" alt=""/></div>
    <div class="box"><img src="images/P_005.jpg" alt=""/></div>
    <div class="box"><img src="images/P_006.jpg" alt=""/></div>
    <div class="box"><img src="images/P_007.jpg" alt=""/></div>
    <div class="box"><img src="images/P_008.jpg" alt=""/></div>
    <div class="box"><img src="images/P_009.jpg" alt=""/></div>
    <div class="box"><img src="images/P_010.jpg" alt=""/></div>
    <div class="box"><img src="images/P_011.jpg" alt=""/></div>
    <div class="box"><img src="images/P_012.jpg" alt=""/></div>
    <div class="box"><img src="images/P_013.jpg" alt=""/></div>
    <div class="box"><img src="images/P_014.jpg" alt=""/></div>
    <div class="box"><img src="images/P_015.jpg" alt=""/></div>
    <div class="box"><img src="images/P_016.jpg" alt=""/></div>
    <div class="box"><img src="images/P_017.jpg" alt=""/></div>
    <div class="box"><img src="images/P_018.jpg" alt=""/></div>
    <div class="box"><img src="images/P_019.jpg" alt=""/></div>
    <div class="box"><img src="images/P_000.jpg" alt=""/></div>
    <div class="box"><img src="images/P_001.jpg" alt=""/></div>
    <div class="box"><img src="images/P_002.jpg" alt=""/></div>
    <div class="box"><img src="images/P_003.jpg" alt=""/></div>
    <div class="box"><img src="images/P_004.jpg" alt=""/></div>
    <div class="box"><img src="images/P_005.jpg" alt=""/></div>
    <div class="box"><img src="images/P_006.jpg" alt=""/></div>
    <div class="box"><img src="images/P_007.jpg" alt=""/></div>
    <div class="box"><img src="images/P_008.jpg" alt=""/></div>
    <div class="box"><img src="images/P_009.jpg" alt=""/></div>
    <div class="box"><img src="images/P_010.jpg" alt=""/></div>
    <div class="box"><img src="images/P_011.jpg" alt=""/></div>
    <div class="box"><img src="images/P_012.jpg" alt=""/></div>
    <div class="box"><img src="images/P_013.jpg" alt=""/></div>
    <div class="box"><img src="images/P_014.jpg" alt=""/></div>
    <div class="box"><img src="images/P_015.jpg" alt=""/></div>
    <div class="box"><img src="images/P_016.jpg" alt=""/></div>
    <div class="box"><img src="images/P_017.jpg" alt=""/></div>
    <div class="box"><img src="images/P_018.jpg" alt=""/></div>
    <div class="box"><img src="images/P_019.jpg" alt=""/></div>

    </div>
    <!--<script>
    //入口函数
    /*window.onload=function(){
    var container=document.getElementById("container");
    var boxes=container.children;
    var pageWidth=window.innerWidth;
    var boxWidth=boxes[0].offsetWidth;
    var column=Math.floor(pageWidth/boxWidth);
    console.log(column);
    }*/

    //获取最小值
    /*function getMin(arr){
    var min={};
    min.index=0;
    min.value=arr[min.index];
    for (var i=0;i<arr.length;i++){
    if (arr[i]<min.value){
    min.value=arr[i];
    min.index=i;
    }
    }
    return min;
    }*/
    /* var arr=[1,2,3,4,5];
    console.log(getMin(arr).value);
    console.log(getMin(arr).index);*/
    </script>-->
    </body>
    </html>

  • 相关阅读:
    linux C程序中获取shell脚本输出(如获取system命令输出)
    Vue实现网页在线拍照和上传 幸福n
    c# thread数线程的创建多线程(一)
    C#开启异步 线程的四种方式(二)
    web学习网站
    C#中的set和get方法
    C# 多线程之Task任务(三)
    Taro3 扫描不同二维码参数不同,但是热启动之后参数不变
    Taro 弹窗阻止小程序滑动穿透(亲测有效) tabbar数据缓存不更新 入口场景值不同
    《Webpack+Babel入门与实例详解》出版了
  • 原文地址:https://www.cnblogs.com/gxw123/p/7047519.html
Copyright © 2011-2022 走看看