花魁直播高品质美女在线视频互动社区 - 花魁直播官方版

 歡迎來到素材無憂網(wǎng),按 + 收藏我們
登錄 注冊(cè) 退出 找回密碼

織夢(mèng)利用Ajax實(shí)現(xiàn)瀑布流下拉無限加載(不帶點(diǎn)擊加載按鈕)

時(shí)間: 2018-12-03 08:13 閱讀: 作者:素材無憂網(wǎng)

瀑布流,又稱瀑布流式布局。是比較流行的一種網(wǎng)站頁面布局,視覺表現(xiàn)為參差不齊的多欄布局,隨著頁面滾動(dòng)條向下滾動(dòng),這種布局還會(huì)不斷加載數(shù)據(jù)塊并附加至當(dāng)前尾部。最早采用此布局的網(wǎng)站是Pinterest,逐漸在國內(nèi)流行開來。國內(nèi)大多數(shù)清新站基本為這類風(fēng)格。采用瀑布流布局的網(wǎng)站操作簡單,在瀏覽網(wǎng)站的時(shí)候只需要輕輕滑動(dòng)一下鼠標(biāo)滾輪,一切的美妙的圖片精彩便可呈現(xiàn)在你面前。

 

瀑布流最實(shí)用于圖片類的網(wǎng)站或者做淘客網(wǎng)站,這樣的功能對(duì)用戶體驗(yàn)極好,也能減少網(wǎng)站的跳出率,對(duì)優(yōu)化也有好處,教大家如何在DedeCMS織夢(mèng)網(wǎng)站中使用這個(gè)功能:

一、找到并打開/plus/list.php文件,在里面找到如下代碼(大概在第12行):

require_once(dirname(__FILE__)."/../include/common.inc.php");

 

在其下面添加如下代碼:

//列表頁瀑布流無限加載代碼
if(isset($_GET['ajax'])){
        $typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//傳遞過來的分類ID
        $page = isset($_GET['page']) ? intval($_GET['page']): 0;//頁碼
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 15;//每頁多少條,也就是一次加載多少條數(shù)據(jù)
        $start = $page>0 ? ($page-1)*$pagesize : 0;//數(shù)據(jù)獲取的起始位置。即limit條件的第一個(gè)參數(shù)。
        $typesql = $typeid ? " WHERE typeid=$typeid" : '';//這個(gè)是用于首頁實(shí)現(xiàn)瀑布流加載,因?yàn)槭醉摷虞d數(shù)據(jù)是無需分類的,所以要加以判斷,如果無需
        $total_sql = "SELECT COUNT(id) as num FROM `dede_archives` $typesql ";
        $temp = $dsql->GetOne($total_sql);
        $total = 0;//數(shù)據(jù)總數(shù)
        $load_num =0;
        if(is_array($temp)){
                $load_num= round(($temp['num']-15)/$pagesize);//要加載的次數(shù),因?yàn)槟J(rèn)已經(jīng)加載了
                $total = $temp['num'];
        }
        $sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule,
        t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath
        FROM `dede_archives` as a JOIN `dede_arctype` AS t ON a.typeid=t.id $typesql ORDER BY id DESC LIMIT $start,$pagesize";
        $dsql->SetQuery($sql);
        $dsql->Execute('list');
        $statu = 0;//是否有數(shù)據(jù),默認(rèn)沒有數(shù)據(jù)
        $data = array();
        $index = 0;
        while($row = $dsql->GetArray("list")){
                $row['info'] = $row['info'] = $row['infos'] = cn_substr($row['description'],160);
                $row['id'] = $row['id'];
                $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],
                $row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
                $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],
                $row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
                $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],
                $row['isdefault'],$row['defaultname'],$row['ispart'],
                $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
                if($row['litpic'] == '-' || $row['litpic'] == ''){
                        $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
                }
                if(!preg_match("#^http:\/\/#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y'){
                        $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
                }
                $row['picname'] = $row['litpic'];//縮略圖
                //$row['stime'] = GetDateMK($row['pubdate']);
                $row['stime'] = date('Y-m-d H:i', $row['pubdate']);
                $row['click'] = $row['click'];
                $row['typelink'] = "".$row['typename']."";//分類鏈
                $row['fulltitle'] = $row['title'];//完整的標(biāo)題
                $row['shorttitle'] = $row['shorttitle'];//副標(biāo)題
                $row['title'] = cn_substr($row['title'], 80);//截取后的標(biāo)題
                $data[$index] = $row;
                $index++;
        }
        if(!empty($data)){
                $statu = 1;//有數(shù)據(jù)
        }
        $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num);
        echo json_encode($result);//返回?cái)?shù)據(jù)
        exit();
}

 

二、則在需要無限加載的模板引用jquery文件,如果網(wǎng)站模版中沒有引用jquery文件,則引用下面這個(gè)js代碼,也可以把JS下載下來放你的網(wǎng)站加載,這里使用百度的外鏈,節(jié)省資源:

<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>

 

三、在模板底部添加如下代碼:

<script type="text/javascript">
var loadConfig = {
        url_api:'/plus/list.php',
        typeid:{dede:field name="typeid"/},
        page:2,
        pagesize:15, //這個(gè)就是滑動(dòng)一次添加幾條信息的參數(shù)設(shè)置
        loading : 0,
        }
function loadMoreApply(){
if(loadConfig.loading == 0){
      var typeid = loadConfig.typeid;
      var page = loadConfig.page;
      var pagesize = loadConfig.pagesize;
      var url = loadConfig.url_api,data={ajax:'pullload',typeid:typeid,page:page,pagesize:pagesize};
      var sTop = document.body.scrollTop || document.documentElement.scrollTop, dHeight = $(document).height(), cHeight = document.documentElement.clientHeight;
      console.log(dHeight);
   
      if (sTop + cHeight >= dHeight - cHeight) {
        loadConfig.loading = 1;
      function ajax(url, data) {
     
        $.ajax({url: url,data: data,async: false,type: 'GET',dataType: 'json',success: function(data) {
          addContent(data);
        }});
      }
      ajax(url,data);
    }
}
}
function addContent (rs){
if(rs.statu== 1){
    var data = rs.list;
    var total = rs.total;
    var arr=[];
    var length = data.length;
    for(var i=0;i<length;i++){
      arr.push('<a href="'+data[i].arcurl+'" title="'+data[i].title+'">');
      arr.push('<dl>');
      arr.push('<dt>');
      arr.push('<img src="'+data[i].picname+'">');
      arr.push('</dt>');
      arr.push('<dd>');
      arr.push('<p>'+data[i].title+'</p>');
      arr.push('<div>');
      arr.push('<span>');
      arr.push(data[i].click);
      arr.push('<em></em>');
      arr.push('</span>');
      arr.push('</div>');
      arr.push('<div>'+data[i].stime+'</div>');
      arr.push('</dd>');
      arr.push('</dl>');
      arr.push('</a>');
    }
    $('.listbox').append(arr.join(''));
    loadConfig.load_num = rs.load_num;
    if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){
      window.removeEventListener('srcoll',loadMoreApply,false);
    }
    loadConfig.page++;
    loadConfig.loading = 0;
}
}
function pullLoad(){
window.addEventListener('scroll', loadMoreApply, false);
}
pullLoad()
checkMobile();
</script>

 

1、上面的代碼中的$('.listbox').append(arr.join(''));里的listbox對(duì)應(yīng)模板內(nèi)列表的外框class屬性。

2、arr.push部分對(duì)應(yīng)的是列表中單篇文章的代碼。

3、這樣就可以想要使用瀑布流無線加載了。

 

4、如果在其它頁調(diào)用的話只需把代碼中的typeid:{dede:field name="typeid"/} 修改為typeid:5(5 就是我們要調(diào)用文章的欄目ID)即可。

注意:很多人在調(diào)試這個(gè)功能的時(shí)候可能把標(biāo)簽寫成了{(lán)dede:list pagesize='10'}{/dede:list} 要用{dede:arclist row="15"}{/dede:arclist}才可以,不然文章是加載不出來的!

 

四、有些網(wǎng)友的需求添加了“加載完成”的提示! 找到如下代碼:

if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){

                        window.removeEventListener('srcoll',loadMoreApply,false);

                }

 

在判斷語句的最后加上如下代碼:

$('<div style="width:100%; float:left; text-align:center; padding: 10px 0px; color:#fff; background:#00aa98; font-size: 16px;">加載完成</div>').insertAfter('.listbox');

 

 加上這段代碼的意思是:當(dāng)文章全部加載完的時(shí)候在所有文章最底部顯示“加載完成”的提示! 在類為“.listbox”的標(biāo)簽下面插入“加載完成”的DIV標(biāo)簽(注意一定要對(duì)應(yīng)替換自己的類名稱)!如圖:

 DedeCMS利用Ajax實(shí)現(xiàn)瀑布流下拉無限加載(不帶點(diǎn)擊加載按鈕)

 

另外在頻道頁,也就是帶多個(gè)下級(jí)子欄目的頻道頁,這個(gè)代碼還有點(diǎn)問題,需要修改plus.php。頻道頁做不到無限加載,是因?yàn)樵谖恼卤砝锩嬷挥凶訖谀?nbsp;typeid 而沒且關(guān)聯(lián)的父欄目 typeid 所以在查詢文章 dede_archives 表的時(shí)候,父欄目 typeid 是沒有對(duì)應(yīng)的文章。所以為了解決這個(gè)問題我在原來的基礎(chǔ)上加了個(gè)判斷,如果當(dāng)傳過來的 typeid 下有子欄目的時(shí)候調(diào)用該欄目下的所以子欄目文章。

 

下面就把修改后的完整代碼貼出來

if(isset($_GET['ajax'])){
        $typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//傳遞過來的分類ID
        $page = isset($_GET['page']) ? intval($_GET['page']): 0;//頁碼
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 15;//每頁多少條,也就是一次加載多少條數(shù)據(jù)
        $start = $page>0 ? ($page-1)*$pagesize : 0;//數(shù)據(jù)獲取的起始位置。即limit條件的第一個(gè)參數(shù)。
        //獲取頻道欄目下的子欄目ID
        $yz_typeid = $dsql->GetOne("SELECT COUNT(id) as num FROM `dede_arctype` WHERE reid=$typeid ");
        if($yz_typeid['num']!=0){
                $sql = "SELECT * FROM `dede_arctype` WHERE reid=$typeid";
                $dsql->SetQuery($sql);
                $dsql->Execute('yz_id');
                $typepdid = "";
                $yz = 1;
                  while($row = $dsql->GetArray("yz_id")){
                          if($yz_typeid['num'] == $yz){
                                $typepdid .= "(typeid=".$row['id'].")";
                        }else{
                                  $typepdid .= "(typeid=".$row['id'].") or ";
                                $yz += 1;
                        }
                  }
                // echo $yz_typeid['num']."+++".$typepdid;
                  //exit();
        }else {
                $typepdid = "typeid=$typeid";
        }
        $typesql = $typeid ? " WHERE $typepdid" : '';//這個(gè)是用于首頁實(shí)現(xiàn)瀑布流加載,因?yàn)槭醉摷虞d數(shù)據(jù)是無需分類的,所以要加以判斷,如果無需
        $total_sql = "SELECT COUNT(id) as num FROM `dede_archives` $typesql ";
        $temp = $dsql->GetOne($total_sql);
        $total = 0;//數(shù)據(jù)總數(shù)
        $load_num =0;
        if(is_array($temp)){
                $load_num= round(($temp['num']-15)/$pagesize);//要加載的次數(shù),因?yàn)槟J(rèn)已經(jīng)加載了
                $total = $temp['num'];
        }
        $sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule,
        t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath
        FROM `dede_archives` as a JOIN `dede_arctype` AS t ON a.typeid=t.id $typesql ORDER BY id DESC LIMIT $start,$pagesize";
        $dsql->SetQuery($sql);
        $dsql->Execute('list');
        $statu = 0;//是否有數(shù)據(jù),默認(rèn)沒有數(shù)據(jù)
        $data = array();
        $index = 0;
        while($row = $dsql->GetArray("list")){
                $row['description'] = cn_substr($row['description'],90);
                $row['id'] = $row['id'];
                $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],
                $row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
                $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],
                $row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
                $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],
                $row['isdefault'],$row['defaultname'],$row['ispart'],
                $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
                $row['waparcurl'] = preg_replace("(.*/m/)","/",$row['arcurl']);
                if($row['litpic'] == '-' || $row['litpic'] == ''){
                        $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
                }
                if(!preg_match("#^http:\/\/#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y'){
                        $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
                }
                $row['picname'] = $row['litpic'];//縮略圖
                //$row['stime'] = GetDateMK($row['pubdate']);
                $row['stime'] = date('Y-m-d H:i', $row['pubdate']);
                $row['click'] = $row['click'];
                $row['typelink'] = "".$row['typename']."";//分類鏈
                $row['fulltitle'] = $row['title'];//完整的標(biāo)題
                $row['shorttitle'] = $row['shorttitle'];//副標(biāo)題
                $row['title'] = cn_substr($row['title'], 80);//截取后的標(biāo)題
                $data[$index] = $row;
                $index++;
        }
        if(!empty($data)){
                $statu = 1;//有數(shù)據(jù)
        }
        $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num);
        echo json_encode($result);//返回?cái)?shù)據(jù)
        exit();
}

版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!

轉(zhuǎn)載請(qǐng)注明: 織夢(mèng)利用Ajax實(shí)現(xiàn)瀑布流下拉無限加載(不帶點(diǎn)擊加載按鈕)

標(biāo)簽:  
相關(guān)文章
模板推薦