jquery实现滑动到底部加载下一页的数据

本文主要介绍了jQuery滑动到底部加载下一页数据的实例代码,需要的朋友可以参考下,希望能帮助到大家。(主要是自己想记录下,下次自个儿用好找点。实诚脸)

具体实现代码如下。

html文件

<body>
        <!-- 列表 -->
        <div id="list"></div>
    </body>
    <script src="static/js/jquery-2.1.4.js"></script>
    <script src="static/js/jquery.scrollLoad.js"></script>
    <script>
        $(function(){
            queryList(); // 查询列表方法
        });
        function queryList(){
            $("#list").load({
                url :"/numberPool/findList",// 后台提供数据的地址
                datas : {
                    name:"参数值"
                },
                callback : function(data) {
                    // 判断返回值是否存在
                    if(data!="undefined"){
                        console.log(data.length);
                        if(data.length>0){
                            var html = '';
                            // 遍历,点出来的值为后台传过来的数据名相等
                            $.each(data, function(i, item){
                                html += '<a href="javaScript:;"><div class="weui-grid__icon">'+item.value+'</div></a>';
                            });
                        }else{
                            var html = '<div class="hint_text">当前没有数据~</div>';
                        }
                    }else{
                        var html = '<div class="hint_text">当前没有数据~</div>';
                    }
                    return html;
                }
            });
        }
    </script>

js文件(忘记从哪下载的已经封装好的emmmmm,感谢封装的大大)

/**
 *  * @param $
 */
(function($) {
    $.fn.load = function(options) {
        options.container = this.selector;
        this.load.options = $.extend({}, this.load.options, {
            pages : 1,
            pageNumber : 1
        }, {dataLoad : false}, options || {});
        $(this.selector).html("");
        var loadingHtml = '<div id="list_cloading" style="text-align:center;display:none;"><img style="width: 30px; margin: 10px auto;" src="/static/images/imgs/loading.gif" /></div>';
        $(options.container).append(loadingHtml);
        try {
            var loadingImage = new Image();
            loadingImage.src = '/static/images/weixiao/imgs/loading.gif';
            loadingImage.onload = function() {
                if (loadingImage.complete == true) {
                    if (loadingImage)
                        $.fn.load.options.getMore();
                }
            }
        } catch (e) {
            this.getMore();
        }
    };

    $.fn.load.options = {
        pageNumber : 1,
        pageSize : 20,
        pages : 1,
        rowCount : 0,
        url : "",
        dataLoad : false, // 数据是否正在加载
        datas : {},
        container : "",
        async : true,
        callback : function() {
        },
        complete : function(){
        },
        getMore : function() {
            if(this.dataLoad){
                return false;
            }
            $.fn.load.options.dataLoad = true;
            if (this.pageNumber > this.pages) {
                return;
            }
            try{
                $.ajax({
                    url : this.url,
                    type : "POST",
                    dataType : "JSON",
                    async : $.fn.load.options.async,
                    timeout : 60000,
                    data : $.extend(true, this.datas, {
                        pageNo : this.pageNumber,
                        pageSize : this.pageSize
                    }),
                    beforeSend : function(xmlHttpRequest) {
                        $("#list_cloading").show();
                    },
                    success : function(data) {
                        if (data) {
                            if ((typeof data) == 'string') {
                                data = $.parseJSON(data);
                            }
                            $.fn.load.options.pages = data.totalpage;
                            $.fn.load.options.rowCount = data.count;
                            var dataList = data.list;
                            var htmls = $.fn.load.options.callback(dataList);
                            $($.fn.load.options.container + " #list_cloading").before(htmls);
                            // 当没有返回pages的时候,将最后一次加载出的数据和pageSize比较,得出pages
                            if (!$.fn.load.options.pages && dataList && dataList.length < $.fn.load.options.pageSize) {
                                $.fn.load.options.pages = $.fn.load.options.pageNumber;
                            }
                            $.fn.load.options.pageNumber++;
                        }
                    },
                    // XMLHttpRequest, textStatus
                    complete : function(XMLHttpReq, status){
                        $("#list_cloading").hide();
                        if($.fn.load.options.complete){
                            $.fn.load.options.complete(XMLHttpReq, status);
                        }
                        $.fn.load.options.dataLoad = false;
                    }
                });
            }catch(e){
                $.fn.load.options.dataLoad = false;
            }
        }
    };

    $(window).scroll(function() {
        if ($(document).scrollTop() > 0 && ($(document).scrollTop() + $(window).height() + 100 >= $(document).height())) {
            $.fn.load.options.getMore();
        }
    });
})(jQuery);

 

Java文件需要:

传入值为:

public JSONObject findList(HttpServletRequest request,int pageNo,int pageSize) {

返回值为:

jsonObject.put("list",list.getData() );(一个集合数据)
jsonObject.put("totalpage",list.getPaging().getTotalPage() );
jsonObject.put("count",list.getPaging().getRowCount() );

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值