瀑布流

思路还是很简单

(1)先初始化一个数组,找到最小的值和它的索引,

(2)根据这两个值设置div的位置(距离左边和顶部)

(3)最后把数组对应索引位置高度加上此节点高度,继续循环

<html><head>
    <meta charset="utf-8">
    <title>瀑布流</title>
    <style>

        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        ul li {
            list-style: none;
        }

        .clearfix {
            *zoom: 1;
        }

        .clearfix:after {
            display: block;
            content: '';
            clear: both;
        }

        a {
            text-decoration: none;
            color: #333;
            font-size: 14px;
        }


      

        .content {
            position: relative;
        }

        .item {
            position: absolute;
            width: 200px;
            margin-right: 10px;
            margin-top: 10px;
            transition: all 1s;
        }

        .h1 {
            height: 200px;
            background-color: #f4b300;
        }

        .h2 {
            height: 300px;
            background-color: #691bb8;
        }

        .h3 {
            height: 400px;
            background-color: #006ac1;
        }

        .img-preview {
            width: 1000px;
            margin: 0 auto;
        }

        .img-row {
            margin-bottom: 8px;
        }

        .img-row:after {
            content: "";
            display: block;
            clear: both;
        }

        .img-box {
            float: left;
        }

        .img-line .img-box:first-child {
            padding-left: 0;
        }

    </style>
</head>
<body>
<div class="content">
    <div class="item h1" style="left: 0px; top: 0px;">1</div>
    <div class="item h3" style="left: 210px; top: 0px;">2</div>
    <div class="item h2" style="left: 420px; top: 0px;">3</div>
    <div class="item h1" style="left: 630px; top: 0px;">4</div>
    <div class="item h1" style="left: 840px; top: 0px;">5</div>
    <div class="item h3" style="left: 1050px; top: 0px;">6</div>
    <div class="item h3" style="left: 1260px; top: 0px;">7</div>
    <div class="item h2" style="left: 1470px; top: 0px;">8</div>
    <div class="item h1" style="left: 0px; top: 210px;">9</div>
    <div class="item h3" style="left: 630px; top: 210px;">10</div>
    <div class="item h3" style="left: 840px; top: 210px;">11</div>
    <div class="item h3" style="left: 420px; top: 310px;">12</div>
    <div class="item h2" style="left: 1470px; top: 310px;">13</div>
    <div class="item h2" style="left: 210px; top: 410px;">14</div>
    <div id="load-more"></div>
</div>
<script src="js/jquery-1.11.1.min.js"></script>
<script>
    $(window).on('resize', function () {
        render();
    });
    function render() {
        var ctWidth = $(window).width()
                , itemWidth = $('.item').outerWidth(true)
                , colNum = Math.floor(ctWidth / itemWidth);
        var colSumHeight = [];
        for (var i = 0; i < colNum; i++) {
            colSumHeight.push(0);
        }
        $('.item').each(function () {
            var $node = $(this);
            var idx = 0,
                    minSumHeight = colSumHeight[0];
            for (var i = 0; i < colSumHeight.length; i++) {
                if (colSumHeight[i] < minSumHeight) {
                    idx = i;
                    minSumHeight = colSumHeight[i];
                }
            }
            $node.css({
                left: itemWidth * idx,
                top: minSumHeight
            });
            colSumHeight[idx] = $node.outerHeight(true) + colSumHeight[idx];
        })
    }
    render();
</script>

</body></html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • html结构 (Emmet) (div.box>div.pic>img[src="images/$.jpg"])*...
    LaBaby_阅读 662评论 0 0
  • html结构 (Emmet)(div.box>div.pic>img[src="images/$.jpg"])*2...
    黎贝卡beka阅读 530评论 1 4
  • github地址little-waterfall-framework-** 这里把整一个瀑布流小框架的实现思路写出...
    Tuberose阅读 6,499评论 12 123
  • 我抽出一张纸巾裹紧春天 擦干净天空又放回了云间 伐木的人走过锋利的刀尖 负伤累累的森林何处呐喊 我揉碎春光种下一座...
    鲟余阅读 501评论 0 7
  • 今天 我本来想打电话给那个老板娘的,但是今天 我还是睡了一天,我的日子就是这样过的 我现在已经 不像过去 那样,觉...
    骆驼和马阅读 169评论 0 0