国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jQuery實現瀑布流的取巧做法分享

2019-11-20 13:26:27
字體:
來源:轉載
供稿:網友

分析:瀑布流,做法有2種

(1)絕對定位方案:每個單元格設置為絕對定位,通過計算,分別設置 top , left 即可實現

(2)浮動方案:弄N列布局(浮動),然后圖片數據,按順序依次插入,如N為3列 ,第一張圖片插入到第一列,第二張圖片插入到第二列,第三張圖片插入到第三列,第四張圖片插入到第一列........這樣循環插入(不能自適應)

CSS與HTML代碼:

復制代碼 代碼如下:

  body,ul,li{margin:0;margin:0;}
    ul{list-style:none;}
    .clearfix:after{visibility:hidden;display:block;font-size:0;content:" ";clear:both; height:0;}.clearfix{*zoom:1}
    /*瀑布流*/
    .wallList{width:860px;}
    .wallList li{float:left;display:inline;margin-right:16px;}
    .wallList li a{background:#eee;border:1px solid #ccc;padding:5px 5px 0 5px;display:block;margin-bottom:10px;}
    .wallList li a:hover{border-color:#f60;}
    .wallList li .name{display:block;text-align:center;padding:8px 0;}
    .loadTips{text-align:center;padding:15px 0;}

復制代碼 代碼如下:

<div class="wallList" id="wallList">
        <ul class="clearfix">
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <p class="loadTips" id="loadTips"><span>正在加載......</span></p>
</div>

使用jQuery實現,大概思路如下:

  (1)獲取N列中 最小的高度值,JS提供的API是Math.min(),但這個API最多只能傳入 2 個參數,所以就需要用aplly來擴展,Math.min.apply(null,[xxx,xxx,xxxx,xxxx])
  (2)給 window 綁定 scroll事件,下拉的時候獲取 $(document).scrollTop() , 當 $(document).scrollTop() 大于 最小的高度值,就ajax請求url,如果有數據,就往頁面插入HTML結構,沒有則提示 “加載完”,然后window解綁此事件

復制代碼 代碼如下:

// 數據格式
    var testJson = {
        "status":1,
        "data":[
            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"            {"href":"http:xxxxxxx","src":"        ]
    }
    var wallPic = function(){
        var $target = $('#wallList'),
            $li = $target.find('li'),
            $tips = $('#loadTips'),
            oTop = 0,//滾動判斷的值
            row = 3,//列數
            page = 1,//ajax請求的頁碼值
            url = 'xxxx', //ajax請求地址
            on_off = true; //插入結構的開關,防止ajax錯誤性多次加載數據
        return{
            fillData:function(callback){
                var _that = this;
                on_off = false;
                /* ajax
                --------------------*/
                // $.get(url,{ page:page,count:30 },function(json){
                //     if(json.status==1){
                //         _that.appendHTML(json.data);
                //         on_off = true;
                //         page++;
                //     }else{
                //         _that.loadedTips();
                //     }
                // },'json');
                /* 模擬測試-設置定時器模擬ajax請求數據
                -----------------------*/
                setTimeout(function(){
                    // 模擬終止
                    if(page==3){
                        _that.loadedTips();
                        return;
                    }
                    _that.appendHTML(testJson.data);
                    on_off = true;
                    page++;                   
                },400);
            },
            appendHTML:function(data){
                var len = data.length,
                    n = 0;
                for(;n<len;n++){
                    var k = 0;
                    n>(row-1)?k=n%row:k=n;
                    $li[k].innerHTML += '<a href="'+data[n].href+'" target="_blank"><img src="'+data[n].src+'" width="'+data[n].width+'" height="'+data[n].height+'" alt="'+data[n].name+'" /><span class="name">'+data[n].name+'</span></a>';
                }
                this.getOTop();
            },
            getOTop:function(){
                oTop = Math.min.apply(null,[$li.eq(0).height(),$li.eq(1).height(),$li.eq(2).height()])+$target.offset().top;
            },
            loadedTips:function(){
                $('#loadTips').find('span').text('數據已加載完');
                setTimeout(function(){
                    $('#loadTips').css({'visibility':'hidden'});
                },200);
                // 解綁事件
                $(window).unbind('scroll',$.proxy(this.scrollEvent,this));
            },
            scrollEvent:function(){
                if($(document).scrollTop()+$(window).height()>oTop&&on_off){
                    this.fillData();                       
                }
            },
            init:function(){
                this.fillData();
                $(window).bind('scroll',$.proxy(this.scrollEvent,this));
            }
        }
    }();
    wallPic.init();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安西县| 东丰县| 丹棱县| 巩义市| 印江| 临清市| 永丰县| 平度市| 哈密市| 苍山县| 安龙县| 邮箱| 永吉县| 娱乐| 同心县| 杂多县| 大名县| 腾冲县| 横山县| 安乡县| 武平县| 阜城县| 开封市| 阿图什市| 乐都县| 东阿县| 西华县| 临漳县| 娄烦县| 乌海市| 金湖县| 兴城市| 西峡县| 林州市| 鹤山市| 自治县| 衡南县| 潮安县| 廊坊市| 龙泉市| 梧州市|