很多用织梦dedecms做图片类的网站都需要用到无限加载这个功能,这个功能需要用到ajax,下面就告诉大家如何实现这样的功能。
首先找到并打开/plus/list.php文件,在里面找到如下代码:
1 |
require_once(dirname(__FILE__)."/../include/common.inc.php"); |
|
在其下面添加如下代码:
01 |
if(isset($_GET['ajax'])){ |
02 |
$typeid = isset($_GET['typeid']) ? intval($_GET['typeid']): 0;//传递过来的分类ID |
03 |
$page = isset($_GET['page']) ? intval($_GET['page']): 0;//页码 |
04 |
$pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']): 15;//每页多少条,也就是一次加载多少条数据 |
05 |
$start = $page>0 ? ($page-1)*$pagesize : 0;//数据获取的起始位置。即limit条件的第一个参数。 |
06 |
$typesql = $typeid ? " WHERE typeid=$typeid" : '';//这个是用于首页实现瀑布流加载,因为首页加载数据是无需分类的,所以要加以判断,如果无需 |
07 |
$total_sql = "SELECT COUNT(id) as num FROM `tufei_archives` $typesql "; |
08 |
$temp = $dsql->GetOne($total_sql); |
12 |
$load_num= round(($temp['num']-15)/$pagesize);//要加载的次数,因为默认已经加载了 |
13 |
$total = $temp['num']; |
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"; |
19 |
$dsql->SetQuery($sql); |
20 |
$dsql->Execute('list'); |
21 |
$statu = 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'; |
37 |
if(!preg_match("#^http:\/\/#i", $row['litpic']) &&$GLOBALS['cfg_multi_site'] == 'Y'){ |
38 |
$row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic']; |
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'];//完整的标题 |
44 |
$row['shorttitle'] = $row['shorttitle'];//副标题 |
45 |
$row['title'] = cn_substr($row['title'], 60);//截取后的标题 |
52 |
$result =array('statu'=>$statu,'list'=>$data,'total'=>$total,'load_num'=>$load_num); |
53 |
echo json_encode($result);//返回数据 |
|
然后在需要使用无线加载的模板里引用下面这个js代码:
并在底部添加如下代码:
01 |
<script type="text/javascript"> |
03 |
url_api:'/plus/list.php', |
09 |
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; |
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) { |
32 |
function addContent (rs){ |
37 |
var length = data.length; |
38 |
for(var i=0;i<length;i++){ |
39 |
arr.push('<a href="'+data[i].arcurl+'">'); |
40 |
arr.push('<div class="item">'); |
41 |
arr.push('<div class="thumbnail">'); |
42 |
arr.push('<img class="img-responsive" src="'+data[i].picname+'" width=460 height=255 />'); |
44 |
arr.push('<div class="caption">'); |
45 |
arr.push('<h3>'+data[i].title+'</h3>'); |
46 |
arr.push('<div class="place">'+data[i].shorttitle+'</div>'); |
52 |
$('.data-list').append(arr.join('')); |
54 |
loadConfig.load_num = rs.load_num; |
56 |
if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){ |
57 |
window.removeEventListener('srcoll',loadMoreApply,false); |
60 |
loadConfig.loading = 0; |
64 |
window.addEventListener('scroll', loadMoreApply, false); |
|
上面的代码中的”$('.data-list').append(arr.join(''));“里的”data-list“对应模板内列表的外框class属性。
arr.push部分对应的是列表代码,
这样就可以使用无线加载了。
如果在列表页只需把代码中的”typeid:0,“ 修改为”typeid:{dede:field name="typeid"/},“即可。