JS滚动条到网页底部自动加载更多内容

<html> <head> <meta charset="gb18030" /> 
    <script type="text/javascript" src="./js/jquery.js"></script> 
    <script> 
        var totalheight = 0; 
        function loadData(){ 
            totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop()); 
            
            if ($(document).height() <= totalheight) {  // 说明滚动条已达底部
                /*这里使用Ajax加载更多内容*/
                var container = $("#container"); // 加载容器
                var data = {}; // 查询参数
                // 当前页
                var currentPage = parseInt(container.find('#currentPage').val());
                // 总页数
                var maxPage = parseInt(container.find('#maxPage').val());
                // 查询日期范围
                var startDate = container.find('#startDate').val());
                var endDate = container.find('#endDate').val());
                data.currentPage = currentPage;
                data.maxPage = maxPage;
                data.startDate =startDate;
                data.endDate = endDate;
                jQuery.ajax({ 
                    type:"POST", 
                    url: "/servlet/query.do", 
                    data:data, 
                    dataType: "json", 
                    beforeSend: function(XMLHttpRequest){ 
                        $("#loading").css('display',''); 
                    }, success:function(response) { 
                        if(response.data){ 
                            for(var i=0, length = response.data.length; i<length; i++){ 
                                var html = response.data[i]; 
                                var test = $(html); 
                                container.append(test); 
                            } 
                            container.find('#currentPage').val(parseInt(currentPage)+1)); 
                            $("#loading").css('display','none'); 
                        } 
                    }, error:function(){ 
                        alert("加载失败"); 
                    } 
                }); 
            } 
        } 
        $(window).scroll( function() { 
            loadData();
        }); 
    </script> 
</head> 
<body>  
    <div id="container">
        这里显示内容
    </div>
    <div id='loading'>
        <img src="./imgs/loading.gif"/>
    </div>
</body> 
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值