原生js的瀑布流写法

以下是一种原生js的瀑布流写法,或许具体实现过程需要慢慢尝试,但是这种思想很重要。
就是对每张图片执行一个循环,在每一行寻找最小的高度,然后将这张图片插入这个最小高度的下方。

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .ct img{
            width: 200px;
            position: absolute;
            transition: all 1s;
            margin: 10px;
        }
    </style>
</head>
<body>
    <div class="ct">
        <div><img src="http://img.hb.aicdn.com/c54df45fba647c28238ede4ebc825d616ce4b8092cb8c-qAUHgv_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/7303810ccf5fa649db989686df95e9ef846aa7e551a92-z1cKrX_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/6c2475d288fca58ff08fc7f2f82dc9c672daddea4e9d8-DAOUW2_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/c1d1554c408f0d597594be7cffece8e06612f4cc1f229-BM4UKl_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/5f73909e54bd694712a4f1bf546ecc37bca074db5db84-rsAmm6_fw658" alt=""></div>
        <div><img src="http://img2.ph.126.net/RkglLXE3wJUgNOofthFUPg==/6631452594769719407.jpg" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/46fdc28bb3442090fa090e3e46f9839ccf1cb83a2d32f-vr95n0_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/f6c625b54b8328ceddff1c55d5a00007c438c6f796f18-OEhi5v_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/b84fc8c7a532fb5b3a5298c8052b9e097c8433e860438-shRN54_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/a4d63dc50abb07377aee13b540bf95a0f1badb6630aa0-L3W94R_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/54a4df446927e9318a0ffd61b54dca67c331a12e241c1-uhvNhf_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/ac1f108db09c28b2a3ad6d03331abec5c21dc8aeca393-47vMPl_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/fe96c8841043f5b8155eec3b36c57657ba9641c914faa-tSgXZ9_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/a5a081d02d674e59c23a48bea9e2f065a4bfdf0c2c855-dZKIMk_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/2a4e698aaa7d1817d52dd172aeda54a6e7a319f9894e7-L7DdFX_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/901fff0b07b7c28f6076d788bca5a7871f5521ca6cda8-lUMBNj_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/725d96417c2148d03b35424454a244ea9bff80e47d425-nH8YMS_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/fde7f978c9bd5905ed5a2fb85050dc7ce9b4a82f4915e-mQaqFx_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/87dad464f1d7a12aca4a9d537455c5a48ba763941a4403-QRNICb_fw658" alt=""></div>
        <div><img src="http://img.hb.aicdn.com/fdfe55280dc189464cc7b8955cd9882b20523c20a34d7-Gontek_fw658" alt=""></div>
    </div>

    //引入jquery
    <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
    <script type="text/javascript">
    //页面打开时先执行一次布局
    waterfall();
    //页面尺寸变化时再重新布局
    $(window).on('resize',function(){
        waterfall();
    });
    function waterfall(){
        var $items = $('.ct img');
        var itemWidth = $items.outerWidth(true);
        var colLength = parseInt($(window).width()/itemWidth);
        var arr = [];
        for (var i = 0; i < colLength; i++) {
            arr[i] = 0;
        }

        $items.each(function(){
        //图片加载完的直接布局,没加载完的等到加载完再布局,防止高度计算错误。
            if(this.complete){
                layout(this);
            }else{
                $(this).on('load',function(){
                    layout(this);
                })
            }
        })

        function layout(ele){
            var minHeight= Math.min.apply(null,arr);
            var minIndex= arr.indexOf(minHeight);   
            $(ele).css({
                top: arr[minIndex],
                left: $(ele).outerWidth(true) * minIndex
            })
            arr[minIndex] += $(ele).outerHeight(true);  
        }
    }
    

效果图:

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,095评论 25 709
  • 电视上正在播放《开学第一课》,一个白头发白胡子的美国人在讲述着对汉字的热爱,他是汉字叔叔理查德,放着收入丰厚的工程...
    松竹友阅读 3,287评论 4 5
  • 终端之间信息传递安全性的保证始终是业务的刚性需求。不同的加密算法针对不同的业务需求,因为公司是金融公司性质,又不是...
    语歌阅读 7,933评论 0 5
  • 引用…… 我想,是在那些每次觉得艰难迷茫失去希望的时刻,我都在内心不断地警醒着自己:为没有办法改变的事情伤神最没有...
    Luuq阅读 1,234评论 0 0

友情链接更多精彩内容