织梦dedecms首页列表页,ajax点击加载更多,瀑布流,首页多栏目切换

2024年05月06日 织梦 dedecms 首页 列表页 ajax 点击 加载 懒猪技术

简化部分主要是栏目切换时,除子第一个默认栏目,其它的全改成ajax,不再生成静态页,而且通过PHP循环,简化了代码.另外加载更多按钮等也修改了触发形式.

一,模板

模板中涉及一个函数.get_url_by_typeid2() 在这里的说明

[html] view plain copy

  1. <div class="container margin-top over-hidden">
  2. <div class="xl12 xb12 over-hidden">
  3. <!-- 切换标签 -->
  4. <style type="text/css">
  5. .slideTxtBox{ width:100%; text-align:left; }
  6. .slideTxtBox .hd{ height:38px; line-height:27px; position:relative; overflow:hidden }
  7. .slideTxtBox .hd ul{left:10px; float:left; position:absolute; top:3px; height:39px;over-flow:hidden;}
  8. .slideTxtBox .hd ul li{height:40px; float:left;color:#fff;padding:0px 7px 0px 6px; cursor:pointer; }
  9. .slideTxtBox .hd ul li a{color:#fff; }
  10. .slideTxtBox .hd ul li.on
  11. {background:url({dede:global.cfg_templets_skin/}/images/bg5.png) repeat-x; }
  12. .slideTxtBox .hd ul li.on a{color:#555}
  13. .slideTxtBox .bd ul{zoom:1;padding-bottom:50px;display:none;position:relative;min-height:100px}
  14. .slideTxtBox .bd li{ height:24px; line-height:24px; }
  15. </style>
  16. <div id="slideTxtBox"class="slideTxtBox padding00"style="overflow:visible !important;">
  17. <!-- 主导航 -->
  18. <div class="hd bg-main pr">
  19. <ul id="tab"class="border-top border-main border-big">
  20. <li class="on"id=""><a href="javascript:"onclick="javascript:loadMoreApply(1,0);">全部</a></li>
  21. {dede:channel type='top' row='50' }
  22. <li id="tab[field:id/]"><a href="javascript:"onclick="javascript:change_tab([field:id/]);loadMoreApply(1,[field:id/]);" >[field:typename/]</a></li>
  23. {/dede:channel}
  24. </ul>
  25. <span class="icon-angle-down text-white width30 padding-left10 ib text-22 top7 right2 bg-main" data-target="#navbar2">
  26. </span>
  27. </div>
  28. <!-- 下拉导航 -->
  29. <div class="navbar-body margin-top6 hidden ib" id="navbar2">
  30. <ul class="nav nav-inline nav-menu ">
  31. {dede:channelartlist typeid="top" row='33' }
  32. <li class="{dede:field.active/}">
  33. <a href="javascript:"onclick="javascript:change_tab({dede:field.typeid/});loadMoreApply(1,{dede:field.typeid/});">
  34. {dede:field name="typename"/}
  35. </a>
  36. </li>
  37. {/dede:channelartlist}
  38. </ul>
  39. </div>
  40. <div class="bd"id="slideTxtBox-bd">
  41. <ul id="0" class="show">
  42. <!-- 全部 -->
  43. {dede:arclist row='2' titlelen='100' orderby='id' }
  44. <div class="bg-fff width-100 ib pr">
  45. <div class="xl12 padding10" >
  46. <div class="media media-x">
  47. <a class="float-left" href="[field:arcurl/]">
  48. <img src="[field:litpic/]" width="80" class="radius" alt="[field:title/]">
  49. </a>
  50. <div class="media-body" style="width:900px;height:;">
  51. <strong>[field:title/]</strong>
  52. <div style="height:5px;"></div>
  53. <!-- 点击 -->
  54. <span>
  55. <span class=" cursor icon-click"> </span>
  56. <script src="/plus/count_list.php?view=yes&aid=[field:id/]" type='text/javascript' language="javascript"></script></span>
  57. <span id="diggNum[field:id/]"><a href="javascript:"class=" text-bbb text-16 width33 " onclick="javascript:postDigg('good',[field:id/])"><span id="digg[field:id/]" class="icon-heart2 "> </span> </a>[field:goodpost/]
  58. </span>
  59. <div style="height:12px;"></div>
  60. <!-- 所属分类 archives-->
  61. <a class="button bg-gray button-little radius-none margin-right2 text-12"href="javascript:"onclick="javascript:change_tab([field:typeid/]);loadMoreApply(1,[field:typeid/]);">[field:typename/]</a>[field:typeid2 function="get_url_by_typeid2(@me)"/]
  62. </div>
  63. </div>
  64. </div>
  65. <a class="button bg-main button-small right5 text-white ib pa text-14"href="javascript:;" onclick="showtip('[field:title/]')"style="top:40px">进入小程序</a>
  66. </div>
  67. {/dede:arclist}
  68. <p class="text-center margin-bottom bottom0 right width-100" ><a onclick="javascript:loadMoreApply(1,0);"href="javascript:"class="button radius-rounded border-main"id="0data">加载更多</a></p>
  69. </ul>
  70. {dede:channel type='top' row='25' }
  71. <!-- [field:typename/] -->
  72. <ul id="[field:id/]">
  73. <p class="text-center bottom0 margin-bottom margin-big-top width-100"><a onclick="javascript:loadMoreApply(1,[field:id/]);"href="javascript:" class="button radius-rounded border-main"id="[field:id/]data">加载更多</a></p>
  74. </ul>
  75. {/dede:channel}
  76. </div>
  77. </div>
  78. <!-- slideTxtBox end -->
  79. <script type="text/javascript">
  80. var tabs=document.getElementById("tab").getElementsByTagName("li");
  81. var divs=document.getElementById("slideTxtBox-bd").getElementsByTagName("ul");
  82. for(var i=0;i<tabs.length;i++){
  83. tabs[i].onclick=function(){change(this);}
  84. }
  85. function change(obj){
  86. for(var i=0;i<tabs.length;i++)
  87. {
  88. if(tabs[i]==obj){
  89. tabs[i].className="on";
  90. divs[i].className="show";
  91. }
  92. else{
  93. tabs[i].className="";
  94. divs[i].className="";
  95. }
  96. }
  97. }
  98. //所属分类切换
  99. function change_tab(typeid){
  100. $("#tab"+typeid).siblings("li").removeClass("on");
  101. $("#tab"+typeid).addClass("on");
  102. $("#"+typeid).siblings("ul").removeClass("show");
  103. $("#"+typeid).addClass("show");
  104. }
  105. </script>
  106. <!-- 切换结束 -->
  107. </div>
  108. </div>
  109. <script type="text/javascript">
  110. $('.icon-angle-down').each(function() {
  111. var e = $(this);
  112. var target = e.attr("data-target");
  113. e.click(function() {
  114. $(target).toggleClass("hidden");
  115. });
  116. });
  117. var loadConfig = {
  118. url_api:'/plus/list.php',
  119. //typeid:{dede:field name="typeid"/},
  120. pagesize:2,
  121. loading : 0
  122. }
  123. var page0=2;
  124. {dede:channel type='top' row='25' }
  125. var page[field:id/]=1;
  126. {/dede:channel}
  127. function loadMoreApply(orderby,typeid){
  128. $("#navbar2").addClass("hidden");
  129. eval(" page =page"+typeid+";");
  130. var pagesize = loadConfig.pagesize;
  131. var url = loadConfig.url_api,
  132. data={ajax:'pullload',typeid:typeid,page:page,pagesize:pagesize,orderby:orderby};
  133. function ajax(url, data) {
  134. $.ajax({url: url,data: data,async: false,type: 'GET',dataType: 'json',success: function(data) {
  135. addContent(data,typeid);
  136. }});
  137. }
  138. ajax(url,data);
  139. }
  140. function addContent (rs,typeid){
  141. if(rs.statu== 1){
  142. var data = rs.list;
  143. var total = rs.total;
  144. var arr=[];
  145. var length = data.length;
  146. for(var i=0;i<length;i++){
  147. arr.push('<div class="bg-fff width-100 ib pr">');
  148. arr.push('<div class="xl12 padding10"><div class="media media-x"><a class="float-left" href="'+data[i].arcurl+'">
  149. <img src="'+data[i].picname+'" width="80" class="radius" alt="'+data[i].title+'"></a><div class="media-body" style="width:900px"><strong>'+data[i].title+'</strong><div style="height:5px;"></div><span> <span class=" cursor icon-click"> </span>'+data[i].click+'</span>');
  150. arr.push('<span id="diggNum'+data[i].id+'"><a href="javascript:"class=" text-bbb text-16" onclick="javascript:postDigg('good','+data[i].id+');"><span id="digg'+data[i].id+'" class="icon-heart2 "> </span> </a>'+data[i].goodpost+'</span> <div style="height:12px;"></div>');
  151. arr.push('<a class="button bg-gray button-little radius-none margin-right2 text-12"href="javascript:"onclick="javascript:change_tab('+data[i].typeid+');loadMoreApply(1,'+data[i].typeid+');">'+data[i].typename+'</a>'+data[i].typeid2+'</div></div></div><a class="button bg-main button-small right5 text-white ib pa text-14"href="javascript:;" onclick="showtip(''+data[i].title+'')"style="top:40px">进入小程序</a></div>');
  152. }
  153. $('#'+typeid).append(arr.join(''));
  154. loadConfig.load_num = rs.load_num;
  155. if(total<page*loadConfig.pagesize || page> loadConfig.load_num){
  156. //$('#'+typeid).append('<p class="text-center margin-top"><a href="javascript:" class="button radius-rounded border-main width-20"id="'+typeid+'data">没有了!</a></p>');
  157. $("#"+typeid+"data").html('没有了!');
  158. }else{
  159. //$('#'+typeid).append('<p class="text-center margin-top"><a href="javascript:" class="button radius-rounded border-main width-20"id="'+typeid+'data">加载更多</a></p>');
  160. }
  161. eval("page"+typeid+" ++;");
  162. }else{
  163. $("#"+typeid+"data").html('没有了!');
  164. }
  165. }
  166. $(document).ready(function(){
  167. loadMoreApply(1,0);
  168. $('.copy-lay .close2').click(function(){
  169. $('.alert2').hide();
  170. });
  171. $('.alert2').click(function(e){
  172. if( $(e.target).parents().hasClass('copy-lay') ){
  173. return;
  174. }else{
  175. $(this).hide();
  176. }
  177. });
  178. });
  179. function showtip(title){
  180. $('#alerttip,#copyBtn').html(title);
  181. $('.alert2').show();
  182. }
  183. </script>
  184. {dede:sql sql='Select * from `myppttype` where id=2'}
  185. <div class="alert2 alert-copy">
  186. <div class="copy-lay" style="[field:other/]">
  187. <span class="close2"></span>
  188. <div class="copy" id="copyBtn"></div>
  189. <div class="copy-layer-img" style="background: url('[field:pic/]') no-repeat; -o-background-size: contain; background-size: contain; "></div>
  190. </div>
  191. </div>
  192. {/dede:sql}



二,,修改,plus/list.PHP 在require_once(dirname(__FILE__)."/../include/common.inc.php");

的下面增加一段

[php] view plain copy

  1. if(isset($_GET['ajax'])){
  2. $typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//传递过来的分类ID
  3. $orderby = isset($_GET['orderby']) ? intval($_GET['orderby']): 1;
  4. //print_r($orderby);exit;
  5. //1id排,2hot排序
  6. $page = isset($_GET['page']) ? intval($_GET['page']): 0;//页码
  7. $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 10;//每页多少条,也就是一次加载多少条数据
  8. $start = $page>0 ? ($page-1)*$pagesize : 0;//数据获取的起始位置。即limit条件的第一个参数。
  9. $ntime2 = gmmktime(0, 0, 0, gmdate('m'), gmdate('d'), gmdate('Y'));
  10. $limitday = $ntime2 - (40 * 24 * 3600);//40天内热文
  11. $orwheres = " senddate > $limitday ";
  12. //$typesql = $typeid ? " WHERE typeid=$typeid" : '';
  13. if($orderby==1){//id降序
  14. $typesql = $typeid ? " WHERE a.arcrank=0 and a.typeid=$typeid or CONCAT
  15. ( ',', a.typeid2, ',' ) LIKE '%,".$typeid.",%' " : '';
  16. $typesql3 = $typeid ? " WHERE arcrank=0 and typeid=$typeid or CONCAT
  17. ( ',', typeid2, ',' ) LIKE '%,".$typeid.",%' " : '';
  18. } else{//hot排序
  19. $typesql = $typeid ? " WHERE a.arcrank=0 and a.typeid=$typeid or CONCAT( ',', a.typeid2, ',' )
  20. LIKE '%,".$typeid.",%' and ".$orwheres : " WHERE ".$orwheres;
  21. $typesql3 = $typeid ? " WHERE arcrank=0 and typeid=$typeid or CONCAT( ',', typeid2, ',' )
  22. LIKE '%,".$typeid.",%' and ".$orwheres : " WHERE ".$orwheres;
  23. }
  24. //print_r($typesql );exit;
  25. //这个是用于首页实现瀑布流加载,
  26. //因为首页加载数据是无需分类的,所以要加以判断,如果无需
  27. $total_sql = "SELECT COUNT(id) as num FROM `archives` $typesql3 ";
  28. $temp = $dsql->GetOne($total_sql);
  29. $total = 0;//数据总数
  30. $load_num =0;
  31. if(is_array($temp)){
  32. $load_num= round(($temp['num']-$pagesize)/$pagesize);//要加载的次数,因为默认已经加载了
  33. $total = $temp['num'];
  34. }
  35. //print_r($total);exit;
  36. if($orderby==1){//新
  37. $sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule,
  38. t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath
  39. FROM `archives` as a JOIN `arctype` AS t ON a.typeid=t.id
  40. $typesql ORDER BY a.id DESC LIMIT $start,$pagesize";
  41. }else{//热门
  42. $sql = "SELECT a.*,t.typedir,t.typename,t.isdefault,t.defaultname,t.namerule,
  43. t.namerule2,t.ispart, t.moresite,t.siteurl,t.sitepath
  44. FROM `archives` as a JOIN `arctype` AS t ON a.typeid=t.id
  45. $typesql ORDER BY a.click DESC LIMIT $start,$pagesize";
  46. }
  47. //echo "$sql";exit;
  48. $dsql->SetQuery($sql);//
  49. $dsql->Execute('list');//die(mysql_error());
  50. $statu = 0;//是否有数据,默认没有数据
  51. $data = array();
  52. $index = 0;
  53. while($row = $dsql->GetArray("list")){
  54. $row['info'] = $row['info'] = $row['infos'] = cn_substr($row['description'],250);
  55. $row['id'] = $row['id'];
  56. $row['typeid2']=get_url_by_typeid2($row['typeid2']);
  57. $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],$row['typeid'],
  58. $row['senddate'],$row['title'],$row['ismake'],
  59. $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],
  60. $row['moresite'],$row['siteurl'],$row['sitepath']);
  61. $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],$row['isdefault'],$row['defaultname'],
  62. $row['ispart'],$row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
  63. if($row['litpic'] == '-' || $row['litpic'] == ''){
  64. $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
  65. }
  66. if(!preg_match("#^http://#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y'){
  67. $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
  68. }
  69. $row['picname'] = $row['litpic'];//缩略图
  70. $row['writer'] = $row['writer'];
  71. $row['click'] = $row['click'];
  72. $row['stime'] = GetDateMK($row['pubdate']);
  73. $row['timeago']= timeago($row['pubdate']);
  74. $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";//分类链
  75. $row['fulltitle'] = $row['title'];//完整的标题
  76. $row['shorttitle'] = $row['shorttitle'];//副标题
  77. $row['title'] = cn_substr($row['title'], 60);//截取后的标题
  78. $data[$index] = $row;
  79. $index++;
  80. }
  81. if(!empty($data)){
  82. $statu = 1;//有数据
  83. }
  84. $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num);
  85. echo json_encode($result);//返回数据
  86. exit();
  87. }


本文链接:http://so.lmcjl.com/news/3938/

展开阅读全文
相关内容