• 瀑布流布局


    组件:

        $.fn.layout = function(params){
            return this.each(function(){
                var options = $.extend({100,lineNum:3,defaultData:'had'},params),
                    $self = $(this);
                var layout = {
                    width : options.width,
                    height : [],
                    end : false,
                    init : function(){
                        this.setHeightArray();
                        this.bind();
                        if(options.defaultData == 'had'){
                            this.append($self.children(),true);
                        }else{
                            this.getData();
                        }                
                    },
                    getData : function(){
                        var me = this;
                        if($self.next('.loading').length <= 0){
                            $self.after('<div class="data-loading loading"><span>加载更多数据</span></div>')
                        }
                        $self.next('.loading').show()
                        $.getJSON(options.url,{},function(d){
                            me.append(d.list)
    
                        },'json')                    
                    },
                    append : function(data,bool){
                        if(data.length > 0){
                           $self.next('.loading').hide() 
                           for(var i = 0 ,len = data.length; i < len; i++){
                                var d = $(data[i]),
                                    height = 0;
                                if(!bool){
                                    $self.append(d);
                                }
                                height = d.outerHeight(true);
                                var pos = this.getPos(height)
                                d.css({top:pos.top,left:pos.left}).fadeIn();
                                $self.height(pos.height);
                            }
                        }else{
                            $self.next('.loading').html('没有更多数据').show()
                            this.end = true;
                        }
                    },
                    setHeightArray : function(){
                        for(var i = 0; i < options.lineNum; i ++){
                            this.height.push(0)
                        }
                    },
                    getPos : function(height){
                        var min = Math.min.apply(null, this.height),
                            index = $.inArray(min,this.height),
                            left = index * this.width,
                            top = min;
                        this.height[index] = this.height[index] + height;
                        return {left : left , top : top, height : Math.max.apply(null, this.height)}
                    },
                    bind : function(){
                        var body = document.body,
                            doc = document.documentElement,
                            me = this;
                        $(window).on('scroll',function(){
                            if(me.end) return;
                            var scrollHeight = Math.max(body.scrollHeight,doc.scrollHeight),
                                scrollTop = Math.max(body.scrollTop,doc.scrollTop);
                                clientHeight = doc.clientHeight;
                            if(scrollHeight < scrollTop + clientHeight + 300){
                                me.getData()
                            }
                        })
                    }
    
                }
    
                layout.init()
            })
        }

    调用方式:

    $(".list").layout({
      200,//单列的宽度
      lineNum:3,//一列展示几排
      url:''
    })

  • 相关阅读:
    mfc crc校验工具
    MFC 配置附加目录
    多线程中如何使用gdb精确定位死锁问题
    符号冲突
    动态库之间单例模式出现多个实例(Linux)
    c++普通函数在头文件定义报重复定义的错误。而class定义不会
    static初始化顺序及延伸
    tcmalloc使用中出现崩溃问题记录
    shell脚本—判断***是否安装
    【1080TI驱动+CUDA10.1+cudnn】安装记录
  • 原文地址:https://www.cnblogs.com/xiaohui108/p/3667267.html
Copyright © 2020-2023  润新知