Typecho实现文章ajax加载更多

第1步

主题目录找到index.php,把主题默认的分页导航的容器换成

<?php $this->pageLink('点击查看更多','next'); ?>

第2步

找到footer.php,前引入js:或写入js文件中

//点击加载更多
jQuery(document).ready(function($) {
    //点击下一页的链接(即那个a标签)
    $('.next').click(function() {
        $this = $(this);
        $this.addClass('loading').text('正在努力加载'); //给a标签加载一个loading的class属性,用来添加加载效果
        var href = $this.attr('href'); //获取下一页的链接地址
        if (href != undefined) { //如果地址存在
            $.ajax({ //发起ajax请求
                url: href,
                //请求的地址就是下一页的链接
                type: 'get',
                //请求类型是get
                error: function(request) {
                    //如果发生错误怎么处理
                },
                success: function(data) { //请求成功
                    $this.removeClass('loading').text('点击查看更多'); //移除loading属性
                    var $res = $(data).find('article'); //从数据中挑出文章数据,请根据实际情况更改
                    //console.log($res);
                    $('.next').before($res.fadeIn(500)); //将数据加载加进posts-loop的标签中。
                    var newhref = $(data).find('.next').attr('href'); //找出新的下一页链接
                    if (newhref != undefined) {
                        $('.next').attr('href', newhref);
                    } else {
                        $('.next').after('<p style="text-align: center;">没有了</p>');
                        $('.next').remove(); //如果没有下一页了,隐藏
                        
                    }
                }
            });
        }
        return false;
    });
});

注意:

以上JS代码选择器根据默认主题修改,根据自身主题文章结构筛选"文章列表数据"。

var $res = $(data).find('article');

参考资料

https://cloud.tencent.com/developer/article/1923557
https://blog.zezeshe.com/archives/infinite-ajax-scroll.html

版权声明:转载或引用请注明出处

点赞 7

添加新评论

验证码: