日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Dede利用ajax無限加載文章

 昵稱61085655 2018-12-06

 很多用織夢(mèng)dedecms做圖片類的網(wǎng)站都需要用到無限加載這個(gè)功能,這個(gè)功能需要用到ajax,下面我就告訴大家如何實(shí)現(xiàn)這樣的功能。

 

首先找到并打開/plus/list.php文件,在里面找到如下代碼:

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

 

在其下面添加如下代碼:

  1. if(isset($_GET['ajax'])){ 
  2.      $typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//傳遞過來的分類ID 
  3.   $page = isset($_GET['page']) ? intval($_GET['page']): 0;//頁碼 
  4.   $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 15;//每頁多少條,也就是一次加載多少條數(shù)據(jù) 
  5.   $start = $page>0 ? ($page-1)*$pagesize : 0;//數(shù)據(jù)獲取的起始位置。即limit條件的第一個(gè)參數(shù)。 
  6.   $typesql = $typeid ? " WHERE typeid=$typeid" : '';//這個(gè)是用于首頁實(shí)現(xiàn)瀑布流加載,因?yàn)槭醉摷虞d數(shù)據(jù)是無需分類的,所以要加以判斷,如果無需 
  7.    $total_sql = "SELECT COUNT(id) as num FROM `tufei_archives`  $typesql "
  8.   $temp = $dsql->GetOne($total_sql); 
  9.   $total = 0;//數(shù)據(jù)總數(shù) 
  10.   $load_num =0
  11.   if(is_array($temp)){ 
  12.     $load_numround(($temp['num']-15)/$pagesize);//要加載的次數(shù),因?yàn)槟J(rèn)已經(jīng)加載了 
  13.     $total = $temp['num']; 
  14.   } 
  15.   $sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule, 
  16.         t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath 
  17. FROM `tufei_archives` as a JOIN `tufei_arctype` AS t ON a.typeid=t.id    $typesql ORDER BY id DESC LIMIT $start,$pagesize"; 
  18. //echo "$sql"; 
  19.    $dsql->SetQuery($sql); 
  20.     $dsql->Execute('list'); 
  21.    $statu = 0;//是否有數(shù)據(jù),默認(rèn)沒有數(shù)據(jù) 
  22.    $data = array(); 
  23.      $index = 0
  24. while($row = $dsql->GetArray("list")){ 
  25.     $row['info'] = $row['info'] = $row['infos'] = cn_substr($row['description'],160); 
  26.      $row['id'] =  $row['id']; 
  27.      $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'], 
  28. $row['typeid'],$row['senddate'],$row['title'],$row['ismake'], 
  29. $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'], 
  30. $row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']); 
  31. $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'], 
  32. $row['isdefault'],$row['defaultname'],$row['ispart'], 
  33.    $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']); 
  34.   if($row['litpic'] == '-' || $row['litpic'] == ''){ 
  35.       $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif'; 
  36.    } 
  37.     if(!preg_match("#^http:\/\/#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y'){ 
  38.     $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic']; 
  39.    } 
  40.   $row['picname'] = $row['litpic'];//縮略圖 
  41.    $row['stime'] = GetDateMK($row['pubdate']); 
  42.   $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";//分類鏈 
  43.   $row['fulltitle'] = $row['title'];//完整的標(biāo)題 
  44.   $row['shorttitle'] = $row['shorttitle'];//副標(biāo)題 
  45.   $row['title'] = cn_substr($row['title'], 60);//截取后的標(biāo)題 
  46.    $data[$index] = $row; 
  47.    $index++; 
  48. if(!empty($data)){ 
  49. $statu = 1;//有數(shù)據(jù) 
  50. $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num); 
  51. echo json_encode($result);//返回?cái)?shù)據(jù) 
  52. exit(); 

 

然后在需要使用無線加載的模板里引用下面這個(gè)js代碼:

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

 

并在底部添加如下代碼:

  1. <script type="text/javascript"> 
  2. var loadConfig = { 
  3.                 url_api:'/plus/list.php', 
  4.                 typeid:0, 
  5.                 page:2, 
  6.                 pagesize:3, 
  7.                 loading : 0, 
  8.                 } 
  9. function  loadMoreApply(){ 
  10.     if(loadConfig.loading == 0){ 
  11.          var typeid = loadConfig.typeid; 
  12.          var page = loadConfig.page; 
  13.          var pagesize = loadConfig.pagesize; 
  14.          var url = loadConfig.url_api,data={ajax:'pullload',typeid:typeid,page:page,pagesize:pagesize}; 
  15.          var sTop = document.body.scrollTop || document.documentElement.scrollTop, dHeight = $(document).height(), cHeight = document.documentElement.clientHeight; 
  16.          console.log(dHeight); 
  17.          if (sTop + cHeight >= dHeight - cHeight) { 
  18.              loadConfig.loading = 1
  19.             function ajax(url, data) { 
  20.                 $.ajax({url: url,data: data,async: false,type: 'GET',dataType: 'json',success: function(data) { 
  21.                  
  22.                     addContent(data);    
  23.                      
  24.                 }}); 
  25.                  
  26.             } 
  27.             ajax(url,data); 
  28.          
  29.         } 
  30.     } 
  31.  function addContent (rs){ 
  32.     if(rs.statu== 1){ 
  33.         var data = rs.list; 
  34.         var total = rs.total; 
  35.         var arr=[]; 
  36.         var length = data.length; 
  37.         for(var i=0;i<length;i++){ 
  38.             arr.push('<a href="'+data[i].arcurl+'">'); 
  39.             arr.push('<div class="item">'); 
  40.             arr.push('<div class="thumbnail">'); 
  41.             arr.push('<img class="img-responsive" src="'+data[i].picname+'" width=460 height=255 />'); 
  42.             arr.push('</div>'); 
  43.             arr.push('<div class="caption">'); 
  44.             arr.push('<h3>'+data[i].title+'</h3>'); 
  45.             arr.push('<div class="place">'+data[i].shorttitle+'</div>'); 
  46.             arr.push('</div>'); 
  47.             arr.push('</div>'); 
  48.             arr.push('</a>'); 
  49.                      
  50.         } 
  51.         $('.data-list').append(arr.join('')); 
  52.      
  53.         loadConfig.load_num = rs.load_num; 
  54.      
  55.         if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){ 
  56.             window.removeEventListener('srcoll',loadMoreApply,false); 
  57.         } 
  58.         loadConfig.page++; 
  59.         loadConfig.loading = 0
  60.     } 
  61. function pullLoad(){ 
  62.     window.addEventListener('scroll', loadMoreApply, false); 
  63. pullLoad() 
  64. checkMobile(); 
  65.  
  66. </script> 

 

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

arr.push部分對(duì)應(yīng)的是列表代碼,

這樣就可以使用無線加載了。

如果在列表頁只需把代碼中的”typeid:0,“ 修改為”typeid:{dede:field name="typeid"/},“即可。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多