最近在学javascript,才慢慢开始读懂JS代码。对HTML前台AJAX加载技术研究了一番,像博客站或者图片站运用AJAX异步无限加载,做成瀑布流,提高用户体验,这是AJAX异步加载的优点。
AJAX异步加载的弊端:属于WEB前端技术,蜘蛛不会抓JS调用的内容,也就是并不利于SEO优化。所以要运用这个功能的话最好有一部分直接调用,另一部分采用AJAX异步加载。
接下来用DedeCMS来做AJAX异步加载WEB前端开发。网上有诸多教程,经过笔者整理,给出详细说明,修正后分享给大家。
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;//每页多少条,也就是一次加载多少条数据 $start = $page>0 ? ($page-1)*$pagesize : 0;//数据获取的起始位置。即limit条件的第一个参数。 $typesql = $typeid ? " WHERE typeid=$typeid" : '';//这个是用于首页实现瀑布流加载,因为首页加载数据是无需分类的,所以要加以判断,如果无需 $total_sql = "SELECT COUNT(id) as num FROM `#分隔符@__archives` $typesql "; $temp = $dsql->GetOne($total_sql); $total = 0;//数据总数 $load_num =0; if(is_array($temp)){ $load_num= round(($temp['num']-15)/$pagesize);//要加载的次数,因为默认已经加载了 $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 `#分隔符@__archives` as a JOIN `#分隔符@__arctype` AS t ON a.typeid=t.id $typesql ORDER BY id DESC LIMIT $start,$pagesize"; //echo "$sql"; $dsql->SetQuery($sql); $dsql->Execute('list'); $statu = 0;//是否有数据,默认没有数据 $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['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";//分类链 $row['fulltitle'] = $row['title'];//完整的标题 $row['shorttitle'] = $row['shorttitle'];//副标题 $row['title'] = cn_substr($row['title'], 60);//截取后的标题 $row['pubdate'] = MyDate('Y-m-d',$row['pubdate']);//处理后的日期如2020-6-12,根据实际情况改写 $data[$index] = $row; $index++; } if(!empty($data)){ $statu = 1;//有数据 } $result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num); echo json_encode($result);//返回数据 exit(); }
注意上面标红的#分隔符@,使用的时候务必删除“分隔符”三个字。
在需要用AJAX异步加载的模板的</head>前增加代码
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> var loadConfig = { url_api:'{dede:global.cfg_cmspath/}/plus/list.php', //修正plus目录 typeid:{dede:field name='typeid'/}, //首页此处是typeid:0, page:2,//从第2页开始调用 pagesize:10,//每滚动一次调用10条 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('<li>'); arr.push('<a href="'+data[i].arcurl+'" class="preview">'); arr.push('<img class="" width="" height="" alt="data[i].title" src="'+data[i].picname+'" />'); arr.push('</a>'); arr.push('[<b>'+data[i].typelink+'</b>]'); arr.push('<a href="'+data[i].arcurl+'" class="title">'+data[i].title+'</a>'); arr.push('<span class="info">'); arr.push('日期:'+data[i].pubdate); arr.push('点击:'+data[i].click); arr.push('好评:'+data[i].scores); arr.push('</span>'); arr.push('<p class="intro">'+data[i].description+'</p>'); arr.push('</li>'); } $('.data-list').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(); </script>
<div class="listbox"> <ul class="data-list"> {dede:arclist row='10'} <li> [field:array runphp='yes']@me = (empty(@me['litpic']) ? "" : "<a href='{@me['arcurl']}' class='preview'><img src='{@me['litpic']}'/></a>"); [/field:array] <b>[field:typelink/]</b> <a href="[field:arcurl/]" class="title">[field:title/]</a> <span class="info"> 日期:[field:pubdate function="MyDate('Y-m-d',@me)"/] 点击:[field:click/] 好评:[field:scores/] </span> <p class="intro">[field:description/]... </p> </li> {/dede:arclist} </ul> </div> <!-- /listbox -->
1、关于发布日期pubdate,直接调用是一串数字,模板里需要用function函数才会显示能读懂的日期,但是ajax就不能直接处理,需在list.php里处理,大家可以根据自己的需要改写。
2、模板里面的<ul class="data-list“>的class名要与js里面的data-list对应,需要把循环部分全部包裹起来。
本文地址:https://www.vi586.com/web/338.html
版权声明:原创文章,版权归重庆SEO吖七所有,欢迎分享本文,支持原创,转载请保留出处