JSFiddle - React, Tailwind, and code Playground

by THR jimmy

HTML

<div id="main">
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_001.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_002.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_003.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_004.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_005.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_006.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_007.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_008.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_009.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_010.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_011.jpg" />
            </div>
        </div>
        <div class="pin">
            <div class="box">
                <img src="http://cued.xunlei.com/demos/publ/img/P_012.jpg"...

SCSS

$font:10px/1.5 sans-serif,"Microsoft YaHei","Arial";
html{
	height: 100%;
	width: 100%;
	font:$font;
}
   #main{
        position: relative;
        &::after{
        	display:block;
        	content:'';
        	clear:both;
        }
    }
    .pin{
        padding: 15px 0 0 15px;
        float:left;
    }
    .box{
        padding: 10px;
        border:1px solid #ccc;
        box-shadow: 0 0 6px #ccc;
        border-radius: 5px;
    }
    .box img{
        width:162px;
        height:auto;
    }

Babel + JSX

var cal = (function() {
    "use strict";
    var $ = function() {
        return document.querySelectorAll.apply(document, arguments);
    }
    var arrHeight = []; //得到分列的高度
    var columns = function() { //计算页面最多可以放多少列
        var bodyW = document.body.clientWidth,
            pinW = $(".pin")[0].offsetWidth;
        return Math.floor(bodyW / pinW);
    }
    var getIndex = function(arr) { //获得最小高度的index
            var minHeight = Math.min.apply(null, arr); //获得最小高度
            for (var i in arr) {
                if (arr[i] === minHeight) {
                    return i;
                }
            }
        }
        //根据列数确定第一排img的高度并放入数组当中。
    var setWidth = function() { //通过列数设置宽度
        var col = columns(), //获得最后一列
            main = $('#main')[0]; //获得罩层
        main.style.width = col * $('.pin')[0].offsetWidth + "px";
    }
    var overLoad = function(ele) {
        var index = getIndex(arrHeight),
            minHeight = Math.min.apply(null, arrHeight), //获取最小高度
            pins = $('.pin'),
            style = ele.style;
        style.position = "absolute";
        style.top = minHeight + "px"; //设置当前元素的高度
        style.left = pins[index].offsetLeft + "px";
        arrHeight[index] += ele.offsetHeight;
    }
    var init = function() {
        var pins = $(".pin"),
            col = columns();
        setWidth(); //设置包裹容器的宽度
        for (var i = 0, pin; pin = pins[i]; i++) {
            if (i < col) { //存储第一排的高度
                arrHeight.push(pin.offsetHeight);
            } else {
                overLoad(pin);  //将元素的位置重排
            }
        }
    }
    window.onload = init;
    //...执行自动更新操作。
    //添加当,滚动到一定位置的时候,添加html节点.
    //得到最低高度和序号,获得临界位置
    //模仿加载数据
    var dataInt = [{
        'src': '1.jpg'
    }, {
        'src': '2.jpg'
    }, {
        'src': '3.jpg'
    }, {
        'src': '4.jpg'
    }, {
        'src': '1.jpg'
    }, {
        'src': '2.jpg'
    }, {
        'src': '3.jpg'
    }, {
        'src': '4.jpg'
    }];

   ...