jQuery实现瀑布流

带你五分钟写出自己的瀑布流

Featured image

  瀑布流,又称瀑布流式布局。是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部

瀑布流的排序规律

瀑布流实现的原理

1、图片的位置摆放

2、 瀑布流图片/div容器的实现是通过对其进行绝对定位来实现的

<!DOCTYPE html>
<html>
<head>
    <title>瀑布流</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <style type="text/css">
    .waterfall{
        max-width: 860px;
        margin: 0 auto;
        position: relative;
    }
    .waterfall img{
        width: 100px;
        margin: 10px;
        position: absolute;
        transition: all .4s;
    }
</style>
</head>

<body>
    <div class="waterfall">
        <img src="http://via.placeholder.com/100x100" alt="300*40">
        <img src="http://via.placeholder.com/100x80" alt="300*100">
        <img src="http://via.placeholder.com/100x150" alt="300*100">
        <img src="http://via.placeholder.com/100x140" alt="300*100">
        <img src="http://via.placeholder.com/100x120" alt="300*100">
        <img src="http://via.placeholder.com/100x110" alt="300*100">
        <img src="http://via.placeholder.com/100x160" alt="300*100">
        <img src="http://via.placeholder.com/100x30" alt="300*100">
        <img src="http://via.placeholder.com/100x50" alt="300*100">
        <img src="http://via.placeholder.com/100x90" alt="300*100">
        <img src="http://via.placeholder.com/100x20" alt="300*100">
        <img src="http://via.placeholder.com/100x60" alt="300*100">
        <img src="http://via.placeholder.com/100x120" alt="300*100">
        <img src="http://via.placeholder.com/100x180" alt="300*100">
        <img src="http://via.placeholder.com/100x200" alt="300*100">
        <img src="http://via.placeholder.com/100x125" alt="300*100">
        <img src="http://via.placeholder.com/100x70" alt="300*100">
        <img src="http://via.placeholder.com/100x120" alt="300*100">
        <img src="http://via.placeholder.com/100x40" alt="300*100">
        <img src="http://via.placeholder.com/100x140" alt="300*100">
    </div>
    <script type="text/javascript">
        var colCount; // 列数
        var colHeightArray; // 保存每列高度的数组
        var imgWidth = $('.waterfall img').outerWidth(true);
        // outerWidth outerWidth() 方法返回第一个匹配元素的外部宽度。该方法包含 padding 和 border。提示:如需包含 margin,请使用 outerWidth(true)
        
        init_cols(); // 设置 colHeightArray 默认数据

        // 当图片加载完成后我们在对其进行排序
        $('.waterfall img').on('load', function() {
            set_position(this)
            
        })

        function init_cols(){ //重置 colHeightArray 数据
            colHeightArray = [];
            colCount = Math.floor($('.waterfall').width() / imgWidth);
            for (var i = 0; i < colCount; i++) {
                colHeightArray[i] = 0;
            }
        }

        function set_position(that) { // 对图片进行排序
            var minValue = colHeightArray[0];
            var minIndex = 0;
            for(let i =0 ; i < colCount; i++) {
                if(colHeightArray[i] < minValue) {
                    minValue = colHeightArray[i];
                    minIndex = i;
                }
            }
            $(that).css({
                left: minIndex * imgWidth,
                top: minValue
            })
            colHeightArray[minIndex] += $(that).outerHeight(true);
        }

        $(window).on('resize', function() {
            init_cols();
            $('.waterfall img').each(function() {
                set_position(this);
            })
        })
    </script>
</body>
</html>