web ,照片墙,瀑布流照片,非常简单,已封装轻量级,可优化,基于jquery,传入数据即可使用

前端 css和 javascript 代码优化

代码开始

css部分

		@keyframes bounceIn {
            100% {
                transform: scale(1.07);
            }
        }
        .box{width:700px;height:2000px;position:relative;}
        .box img{animation: bounceIn 1s ease-in 2 alternate;}

html部分

	<div class="box">

    </div>
    <script src="./static/jquery2.1.4.min.js"></script>

js部分

		function CatCreateWaterFall(o){
            this.dom = $(o.dom);
            this.columns = o.columns;
            this.data = o.data;
            this.width = this.dom.width();
            this.height = o.height;
            this.itemWidth = this.width/this.columns;
            this.init();
            var that = this;
            setTimeout(() => {
                that.fall();
            }, 1000);
        }
        CatCreateWaterFall.prototype.init = function(){
            var that = this;
            for(let i = 0;i<that.data.length;i++){
                var img = new Image();
                img.src = that.data[i];// 根据数据结构不同,这里可能会更改  eg: that.data[i].path
                $(img).appendTo(that.dom);
                // if(i==that.data.length-1){img.onload = function(){
                // 	setTimeout(that.fall(),2000)
                // }}
            }
        }
        CatCreateWaterFall.prototype.fall = function(){
            var that = this,arr = [];
            this.dom.find("img").each(function (i) {
                var height = $(this).height();
                var width = $(this).width();
                var bi = that.itemWidth / width;
                var boxheight = parseInt(height * bi);
                if (i < that.columns) {
                    $(this).css({
                        width:that.itemWidth,
                        top: 0,
                        left: (that.itemWidth) * i,
                        position:"absolute"
                    });
                    arr.push(boxheight);
                } else {
                    var minHeight = arr[0];
                    var index = 0;
                    for (var j = 0; j < arr.length; j++) {
                        if (minHeight > arr[j]) {
                            minHeight = arr[j];
                            index = j;
                        }
                    }
                    $(this).css({
                        width:that.itemWidth,
                        top: arr[index],
                        left:that.dom.find("img").eq(index).css("left"),
                        position:"absolute"
                    });
                    arr[index] = arr[index] + boxheight;
                }
            });
        }

使用(js)

		let o = {
            dom:".box",
            columns:3,
            data:["./resources/01.jpg","./resources/02.jpg","./resources/03.jpg","./resources/04.jpg","./resources/05.jpg","./resources/06.jpg","./resources/07.jpg"],
            width:700,
            height:2000,

        }
        new CatCreateWaterFall(o);
        // var water = new CatCreateWaterFall(o);

补充

    超轻量级,根本不需要懂逻辑,用就行,适合新手,优化的话 fall 方法或者 init 方法都行。
    ~~ 欢迎一键三连哦! ~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值