Masonry JS瀑布流布局

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Masonry JS瀑布流布局</title>

<style>
li{list-style:none;}
ul{
    width:90%;
    margin:0 auto;
}
ul li{margin: 10px;}
.col-w1{ width:200px;}
.col-w2{width: 400px;}
.col-t1{width: 30%;}
ul li[class*=col-] img{
    border: 0;
    width: 100%;
}
.stamp{position: absolute; background: red;}
.stamp1{left: 45%; width: 50px;height: 600px;background: red;}
.stamp2{left: 40%; top: 400px; width: 20%;height: 50px;background: blue;}

</style>

</head>

<body>
    <button id="onStamp">戳记开关</button>
    <ul class="grid">
        <div class="stamp stamp1"></div>
        <li class="item col-w1"><img src="https://i0.hippopx.com/photos/477/768/839/swimmer-sport-swim-water-preview.jpg"><br><span>公路</span></li>
        <li class="item col-w2"><img src="https://i0.hippopx.com/photos/740/837/204/spices-cinnamon-sticks-odor-aroma-preview.jpg"><br><span style="font-size: 30px;">标题名2</span>标题名2标题名2</li>
		<li class="item col-w1"><img src="https://cdn.pixabay.com/photo/2018/01/01/01/57/woman-3053492__340.jpg"><br>标题名</li>
        <div class="stamp stamp2"></div>
		<li class="item col-t1"><img src="https://cdn.pixabay.com/photo/2018/02/01/12/51/mammal-3123179__340.jpg"></li>
		<li class="item col-w1"><img src="https://cdn.pixabay.com/photo/2018/05/04/10/30/team-3373638__340.jpg"></li>
		<li class="item col-w1"><img src="https://i0.hippopx.com/photos/971/691/698/mobile-phone-smartphone-3d-manipulation-preview.jpg"></li>
		<li class="item col-w2"><img src="https://i0.hippopx.com/photos/477/768/839/swimmer-sport-swim-water-preview.jpg">公路</li>
		<li class="item col-w1"><img src="https://cdn.pixabay.com/photo/2018/01/01/01/57/woman-3053492__340.jpg">标题名</li>
		<li class="item"><img src="https://i0.hippopx.com/photos/20/987/594/woman-young-rain-pond-preview.jpg"><br>标题名2标题名2标题名2</li>
		<li class="item col-w2"><img src="https://cdn.pixabay.com/photo/2018/05/04/10/30/team-3373638__340.jpg"></li>
		<li class="item"><img src="https://cdn.pixabay.com/photo/2018/01/09/22/51/rose-3072698__340.jpg"></li>
        <li class="item"><img src="https://cdn.pixabay.com/photo/2018/02/01/12/51/mammal-3123179__340.jpg"></li>
        <li class="item"><img src="https://i0.hippopx.com/photos/320/918/427/sky-clouds-sunlight-dark-preview.jpg"></li>
        <li class="item"><img src="https://i0.hippopx.com/photos/179/171/625/sparkler-holding-hands-firework-preview.jpg"></li>
    </ul>

    <!-- 图片加载侦听插件 官网:https://masonry.desandro.com/-->
    <script src="imagesloaded.pkgd.min.js"></script>
    <!-- 瀑布流布局插件 官网:https://imagesloaded.desandro.com/-->
    <script src="masonry.pkgd.min.js"></script>
    <script type="text/javascript">
        //排除非item的不计算在内
        // var elem = document.querySelector('.grid');
        // var msnry = new Masonry( elem, {
        //     // options
        //     itemSelector: '.item',
        //     columnWidth: 200
        // });

        // element argument can be a selector string
        // for an individual element
        var msnry = new Masonry( '.grid', {
            itemSelector: '.item',
            stamp: '.stamp'
            // horizontalOrder: true //左至右
        });

        //每加载完毕一张图调用
        var imgLoad = imagesLoaded ('.grid');
        imgLoad.on( 'progress', function( instance, image ) {
            msnry.layout();
        });

        //----------------------------------
        //后续新加入元素处理:
        var node=document.createElement("LI");
        node.innerHTML='<img src="https://i0.hippopx.com/photos/20/987/594/woman-young-rain-pond-preview.jpg"><br>我是新加入的';
        node.classList.add("item")
        // console.log(node.childNodes);
        document.querySelector('.grid').appendChild( node );
        msnry.appended( node );
        msnry.layout();

        //----------------------------------
        //戳记开关事件:
        var isStamped = true;
        var stamp =  document.querySelectorAll('.stamp');
        document.getElementById("onStamp").onclick = function(){
            if(isStamped){
                msnry.unstamp('.stamp');
            }else{
                msnry.stamp('.stamp');
            }
            msnry.layout();
            isStamped = !isStamped;
        }

    </script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值