利用ajax实现织梦dedecms瀑布流无限加载功能

该功能主要用到AJAX技术! 
一、首先找到并打开/plus/list.PHP文件,在里面找到如下代码:

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

在其代码下面添加如下代码:

//列表页瀑布流无限加载代码
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";
    $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['stime'] = date('Y-m-d H:i', $row['pubdate']);
        $row['click'] = $row['click'];
        $row['typelink'] = "".$row['typename']."";//分类链
        $row['fulltitle'] = $row['title'];//完整的标题
        $row['shorttitle'] = $row['shorttitle'];//副标题
        $row['title'] = cn_substr($row['title'], 80);//截取后的标题
        $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();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

二、然后在需要使用瀑布流无线加载的模板里引用下面这个js代码(这个js大家应该不是很陌生,只要有用到JQ库函数的话一定要引用这个js):

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

三、并在底部添加如下代码:

<script type="text/javascript">
var loadConfig = {
                url_api:'/plus/list.php',
                typeid:{dede:field name="typeid"/},
                page:2,
                pagesize:2,  //这个就是滑动一次添加几条信息的参数设置
                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 class="badcat-cell mui-media">');
            arr.push('<a href="'+data[i].arcurl+'">');
            arr.push('<img class="mui-media-object mui-pull-right" src="'+data[i].picname+'">');
            arr.push('<div class="mui-media-body">');
            arr.push(''+data[i].title+'');
            arr.push('<p class="mui-ellipsis">'+data[i].info+'.</p>');
            arr.push('</div>');
            arr.push('</a>');
            arr.push('</li>');
        }
        $('.badcat').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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
上面的代码中的$('.badcat').append(arr.join(''));里的badcat对应模板内列表的外框class属性。
arr.push部分对应的是列表中单篇文章的代码。
这样就可以想要使用瀑布流无线加载了。
如果在其它页调用的话只需把代码中的typeid:{dede:field name="typeid"/} 修改为typeid:5(5 就是我们要调用文章的栏目ID)即可。
注意:很多人在调试这个功能的时候可能把标签写成了
{dede:list pagesize='10'}{/dede:list} 
 
 
  • 1
  • 1

要用

{dede:arclist row="15"}{/dede:arclist}
 
 
  • 1
  • 1

才可以,不然文章是加载不出来的!

基于有些网友的需求添加了“加载完成”的提示! 找到如下代码

if(total<loadConfig.page*loadConfig.pagesize || loadConfig.page > loadConfig.load_num){
            window.removeEventListener('srcoll',loadMoreApply,false);
 
 
  • 1
  • 2
  • 1
  • 2

在判断语句的最后加上如下代码!

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

加上这段代码的意思是:当文章全部加载完的时候在所有文章最底部显示“加载完成”的提示! 代码的意思是:在类为“.badcat”的标签下面插入 “加载完成”的DIV标签!

文章由GIF动态图http://www.23o.cn)编辑整理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值