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

首頁 > 開發(fā) > PHP > 正文

PHP結(jié)合Jquery和ajax實現(xiàn)瀑布流特效

2024-05-04 23:41:58
字體:
供稿:網(wǎng)友
php+ajax+jquery實現(xiàn)無限瀑布流布局 寬度是一定的高度不定的瀑布流布局 也可以說是無縫拼圖 當(dāng)瀏覽器滾動到底部時候自動加載圖片,非常的實用,需要的小伙伴可以參考下。
 

不廢話,直接上代碼,

前臺:

<?php $category=$this->getMyVal('category',$_GET);$xiaohuaList=Xiaohua::model()->getXiaohao($category); //打開頁面默認(rèn)顯示的數(shù)據(jù)?><div id="waterfall">  <?php foreach ($xiaohuaList as $xiaohua):?>    <?php $q_id=$xiaohua->id;?>    <div class="cell m-bg item-h border_h">      <div class="border-solid-b padding-b-5 text-center"><span class="g-bg glyphicon glyphicon-sunglasses margin-r-5" aria-hidden="true"></span><strong class="color-5 fx_t_<?php echo $q_id;?>"><?php echo CHtml::encode($xiaohua->title);?></strong></div>      <div class="padding-t-5 fx_c_<?php echo $q_id;?>"><?php echo $xiaohua->content;?></div>      <div class="padding-t-5 text-right"><span onclick="fx(<?php echo $q_id;?>);" class="fx cursor_p" data-id="<?php echo $q_id;?>"><span class="g-bg glyphicon glyphicon-share-alt margin-r-5" aria-hidden="true"></span>分享</span></div>    </div>  <?php endforeach;?></div><script>var opt={ getResource:function(index,render){//index為已加載次數(shù),render為渲染接口函數(shù),接受一個dom集合或jquery對象作為參數(shù)。通過ajax等異步方法得到的數(shù)據(jù)可以傳入該接口進行渲染,如 render(elem)   var html='';   var _url='<?php echo $this->createUrl('listXiaohua');?>';   $.ajax({    type: "get",    url: _url,    dataType : "json",    async:false,    success: function(data){      for( var i in data){        var q_id=data[i].id;        html+='<div class="cell m-bg item-h border_h"><div class="border-solid-b padding-b-5 text-center"><span class="g-bg glyphicon glyphicon-sunglasses margin-r-5" aria-hidden="true"></span><strong class="color-5 fx_t_'+q_id+'">'+data[i].title+'</strong></div><div class="padding-t-5 fx_c_'+q_id+'">'+data[i].content+'</div>'           +'<div class="padding-t-5 text-right"><span onclick="fx('+q_id+');" class="fx cursor_p" data-id="'+q_id+'"><span class="g-bg glyphicon glyphicon-share-alt margin-r-5" aria-hidden="true"></span>分享</span></div></div>';      }   }});   return $(html); }, column_width:376, column_space:10, auto_imgHeight:true, insert_type:1}$('#waterfall').waterfall(opt);</script>

后臺:

public function actionListXiaohua() {  $xiaohuaList=Xiaohua::model()->getXiaohua();//獲取笑話信息  echo CJSON::encode($xiaohuaList);}

js:

(function($){  var  //參數(shù)  setting={   column_width:240,//列寬   column_className:'waterfall_column',//列的類名   column_space:2,//列間距   cell_selector:'.cell',//要排列的磚塊的選擇器,context為整個外部容器   img_selector:'img',//要加載的圖片的選擇器   auto_imgHeight:true,//是否需要自動計算圖片的高度   fadein:true,//是否漸顯載入   fadein_speed:600,//漸顯速率,單位毫秒   insert_type:1, //單元格插入方式,1為插入最短那列,2為按序輪流插入   getResource:function(index){ } //獲取動態(tài)資源函數(shù),必須返回一個磚塊元素集合,傳入?yún)?shù)為加載的次數(shù)  },  //  waterfall=$.waterfall={},//對外信息對象  $waterfall=null;//容器  waterfall.load_index=0, //加載次數(shù)  $.fn.extend({    waterfall:function(opt){     opt=opt||{};     setting=$.extend(setting,opt);     $waterfall=waterfall.$waterfall=$(this);     waterfall.$columns=creatColumn();     render($(this).find(setting.cell_selector).detach(),false); //重排已存在元素時強制不漸顯     waterfall._scrollTimer2=null;     $(window).bind('scroll',function(){       clearTimeout(waterfall._scrollTimer2);       waterfall._scrollTimer2=setTimeout(onScroll,300);     });     waterfall._scrollTimer3=null;     $(window).bind('resize',function(){       clearTimeout(waterfall._scrollTimer3);       waterfall._scrollTimer3=setTimeout(onResize,300);     });    }  });  function creatColumn(){//創(chuàng)建列   waterfall.column_num=calculateColumns();//列數(shù)   //循環(huán)創(chuàng)建列   var html='';   for(var i=0;i<waterfall.column_num;i++){     html+='<div class="'+setting.column_className+'" style="width:'+setting.column_width+'px; display:inline-block; *display:inline;zoom:1; margin-left:'+setting.column_space/2+'px;margin-right:'+setting.column_space/2+'px; vertical-align:top; overflow:hidden"></div>';   }   $waterfall.prepend(html);//插入列   return $('.'+setting.column_className,$waterfall);//列集合  }  function calculateColumns(){//計算需要的列數(shù)   var num=Math.floor(($waterfall.innerWidth())/(setting.column_width+setting.column_space));   if(num<1){ num=1; } //保證至少有一列   return num;  }  function render(elements,fadein){//渲染元素   if(!$(elements).length) return;//沒有元素   var $columns = waterfall.$columns;   $(elements).each(function(i){     if(!setting.auto_imgHeight||setting.insert_type==2){//如果給出了圖片高度,或者是按順序插入,則不必等圖片加載完就能計算列的高度了       if(setting.insert_type==1){        insert($(elements).eq(i),setting.fadein&&fadein);//插入元素       }else if(setting.insert_type==2){        insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素       }       return true;//continue     }     if($(this)[0].nodeName.toLowerCase()=='img'||$(this).find(setting.img_selector).length>0){//本身是圖片或含有圖片       var image=new Image;       var src=$(this)[0].nodeName.toLowerCase()=='img'?$(this).attr('src'):$(this).find(setting.img_selector).attr('src');       image.onload=function(){//圖片加載后才能自動計算出尺寸         image.onreadystatechange=null;         if(setting.insert_type==1){           insert($(elements).eq(i),setting.fadein&&fadein);//插入元素         }else if(setting.insert_type==2){           insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素         }         image=null;       }       image.onreadystatechange=function(){//處理IE等瀏覽器的緩存問題:圖片緩存后不會再觸發(fā)onload事件         if(image.readyState == "complete"){           image.onload=null;           if(setting.insert_type==1){            insert($(elements).eq(i),setting.fadein&&fadein);//插入元素           }else if(setting.insert_type==2){            insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素           }           image=null;         }       }       image.src=src;     }else{//不用考慮圖片加載       if(setting.insert_type==1){         insert($(elements).eq(i),setting.fadein&&fadein);//插入元素       }else if(setting.insert_type==2){         insert2($(elements).eq(i),i,setting.fadein&&fadein);//插入元素       }     }   });  }  function public_render(elems){//ajax得到元素的渲染接口   render(elems,true);  }  function insert($element,fadein){//把元素插入最短列   if(fadein){//漸顯     $element.css('opacity',0).appendTo(waterfall.$columns.eq(calculateLowest())).fadeTo(setting.fadein_speed,1);   }else{//不漸顯     $element.appendTo(waterfall.$columns.eq(calculateLowest()));   }  }  function insert2($element,i,fadein){//按序輪流插入元素   if(fadein){//漸顯     $element.css('opacity',0).appendTo(waterfall.$columns.eq(i%waterfall.column_num)).fadeTo(setting.fadein_speed,1);   }else{//不漸顯     $element.appendTo(waterfall.$columns.eq(i%waterfall.column_num));   }  }  function calculateLowest(){//計算最短的那列的索引   var min=waterfall.$columns.eq(0).outerHeight(),min_key=0;   waterfall.$columns.each(function(i){     if($(this).outerHeight()<min){      min=$(this).outerHeight();      min_key=i;     }   });   return min_key;  }  function getElements(){//獲取資源   $.waterfall.load_index++;   return setting.getResource($.waterfall.load_index,public_render);  }  waterfall._scrollTimer=null;//延遲滾動加載計時器  function onScroll(){//滾動加載   clearTimeout(waterfall._scrollTimer);   waterfall._scrollTimer=setTimeout(function(){     var $lowest_column=waterfall.$columns.eq(calculateLowest());//最短列     var bottom=$lowest_column.offset().top+$lowest_column.outerHeight();//最短列底部距離瀏覽器窗口頂部的距離     var scrollTop=document.documentElement.scrollTop||document.body.scrollTop||0;//滾動條距離     var windowHeight=document.documentElement.clientHeight||document.body.clientHeight||0;//窗口高度     if(scrollTop>=bottom-windowHeight){       render(getElements(),true);     }   },100);  }  function onResize(){//窗口縮放時重新排列   if(calculateColumns()==waterfall.column_num) return; //列數(shù)未改變,不需要重排   var $cells=waterfall.$waterfall.find(setting.cell_selector);   waterfall.$columns.remove();   waterfall.$columns=creatColumn();   render($cells,false); //重排已有元素時強制不漸顯  }})(jQuery);

好了,全部完成了。

再給大家分享一個吧

<!doctype html><html><head><meta charset="utf-8"><title>定寬Jquery+AJAX+JSON瀑布流布局(每行代碼都有詳細(xì)注釋)</title><style type="text/css">body, ul, li, h3 { margin: 0; padding: 0; list-style: none; font: bold 12px "微軟雅黑"; }/*瀑布流布局樣式*/#lxf-box { position: relative; width: 1000px; margin:0 auto;}#lxf-box li { background: #fff; border: solid 1px #ccc; text-align: center; padding: 10px; float: left;}h3 { padding-top: 8px; }img { width: 200px; height: auto; display: block; border: 0 }/*css3動畫 注由于是css3制作的所以兼容性不保證 要想兼容性好 請自己做成gif動畫加載圖*//*li { -webkit-transition: all .7s ease-out .1s; -moz-transition: all .7s ease-out; -o-transition: all .7s ease-out .1s; transition: all .7s ease-out .1s }*/#loading { display:none; line-height: 30px; background: #000; color:#fff; text-align: center; height: 30px; width: 100%; position:fixed; bottom:0; opacity:0.8;}</style><script src="/templets/niu/js/jquery.min.js" type="text/javascript"></script></head><body><h1 color="red">預(yù)覽無效果請刷新</h1><ul id="lxf-box"> <li><a href="div_css/342.html"><img src="/uploads/allimg/120814/1-120Q411544TX.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/ajax/237.html"><img src="/uploads/allimg/120801/1-120P1223013157.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/js_ad/271.html/"><img src="/uploads/allimg/120808/1-120PP00915a2.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/js_texiao/312.html/"><img src="/uploads/allimg/120812/1-120Q2150022G8.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/js_pic/191.html/"><img src="/uploads/allimg/120722/1-120H2144003129.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/js_pic/318.html/"><img src="/uploads/allimg/120812/1-120Q2161941b2.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/div_css/341.html/"><img src="/uploads/allimg/120814/1-120Q4113240U2.jpg"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/div_css/350.html/"><img src="/uploads/allimg/120814/125411K11-2.png"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/div_css/350.html/"><img src="/uploads/allimg/120814/1254113249-12.png"></a>  <h3>圖片標(biāo)題</h3> </li> <li><a href="/div_css/349.html/"><img src="/uploads/allimg/120814/12500a292-1.png"></a>  <h3>圖片標(biāo)題</h3> </li> <li>img src="/uploads/allimg/120813/1-120Q3145U0938.jpg">  <h3>圖片標(biāo)題</h3> </li> <li><a href="/div_css/344.html/"><img src="/uploads/allimg/120814/12353B521-0.jpg"></a>  <h3>圖片標(biāo)題</h3> </li></ul><div id="loading">正在加載……</div><script>/*原理:1.把所有的li的高度值放到數(shù)組里面   2.第一行的top都為0   3.計算高度值最小的值是哪個li   4.把接下來的li放到那個li的下面編寫時間:2012年6月9日*/ function iiwnet(){//定義成函數(shù)便于調(diào)用var wrap = document.getElementById("lxf-box")var margin = 10;//這里設(shè)置間距var li=$("li");//這里是區(qū)塊名稱var li_W = li[0].offsetWidth+margin;//取區(qū)塊的實際寬度(包含間距,這里使用源生的offsetWidth函數(shù),不適用jQuery的width()函數(shù)是因為它不能取得實際寬度,例如元素內(nèi)有pandding就不行了)var h=[];//記錄區(qū)塊高度的數(shù)組  li.css("position","absolute");  var n = wrap.offsetWidth/li_W|0;//容器的寬度除以區(qū)塊寬度就是一行能放幾個區(qū)塊  for(var i = 0;i < li.length;i++) {//有多少個li就循環(huán)多少次    li_H = li[i].offsetHeight;//獲取每個li的高度    if(i < n) {//n是一行最多的li,所以小于n就是第一行了      h[i]=li_H;//把每個li放到數(shù)組里面      li.eq(i).css("top",0);//第一行的Li的top值為0      li.eq(i).css("left",i * li_W);//第i個li的左坐標(biāo)就是i*li的寬度      }    else{      min_H =Math.min.apply(null,h) ;//取得數(shù)組中的最小值,區(qū)塊中高度值最小的那個      minKey = getarraykey(h, min_H);//最小的值對應(yīng)的指針      h[minKey] += li_H+margin ;//加上新高度后更新高度值      li.eq(i).css("top",min_H+margin);//先得到高度最小的Li,然后把接下來的li放到它的下面      li.eq(i).css("left",minKey * li_W); //第i個li的左坐標(biāo)就是i*li的寬度    }    $("h3").eq(i).text("編號:"+i+",高度:"+li_H);//把區(qū)塊的序號和它的高度值寫入對應(yīng)的區(qū)塊H3標(biāo)題里面    $("li").animate({opacity:1});  }}/* 使用for in運算返回數(shù)組中某一值的對應(yīng)項數(shù)(比如算出最小的高度值是數(shù)組里面的第幾個) */function getarraykey(s, v) {for(k in s) {if(s[k] == v) {return k;}}}/*這里一定要用onload,因為圖片不加載完就不知道高度值*/window.onload = function() {iiwnet();};/*瀏覽器窗口改變時也運行函數(shù)*/window.onresize = function() {iiwnet();}; /**********************************************************************//*無限加載*/var i=1;function getMore(){  $("#loading").show();    var json = "http://m.survivalescaperooms.com/images/jstx/img.js";    $.getJSON(json, function(data){         $.each(data,function(i){        var url=data[i].url;          var html="<li style='opacity:0'><a href='http://m.survivalescaperooms.com/'><img src="+url+" ></a><h3>圖片標(biāo)題</h3></li>";          $("#lxf-box").append(html);             $("#loading").hide();        });        iiwnet();        i=1    });  };  /*滾動到底部的時候*/   $(window).bind("scroll",function(){  if( $(document).scrollTop() + $(window).height() > $(document).height() - 10 && i==1) {    i=0;    getMore();         }   });</script></body></html><p align="center"><font color=red>如果運行效果無非顯示請點擊【刷新頁面】</font><br/>【<a href="http://m.survivalescaperooms.com" target="_blank"><font color="#666666">返回首頁</font></a>】【<a href="javascript:this.location.reload()"><font color="#666666">刷新本頁</font></a>】【<a href="javascript:window.scroll(0,0)"><font color="#666666">返回頂部</font></a>】【<a href="javascript:window.close()"><font color="#666666">關(guān)閉本頁</font></a>】</p>              


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安庆市| 文登市| 略阳县| 盈江县| 南漳县| 泸溪县| 惠水县| 酉阳| 黎平县| 东安县| 宁强县| 苍溪县| 常熟市| 沭阳县| 梅河口市| 文成县| 兴安盟| 乌恰县| 城口县| 二连浩特市| 小金县| 辽中县| 长垣县| 瓮安县| 兴义市| 伽师县| 嵩明县| 通渭县| 阜康市| 揭西县| 内黄县| 怀远县| 厦门市| 静安区| 台中市| 宁乡县| 阿勒泰市| 四子王旗| 石首市| 乐业县| 大英县|