jquery开发无限滚动加载插件教程附源码
日期:2018-03-14
            来源:程序思维浏览:2139次
            
                现在网上的无限滚动加载插件很多,但是他们是如何制作的呢?用到了什么技术?现在就让我来给大家揭晓一下吧:
思路解析:
1、监听滚动条是否到了页面最底部。
2、到了页面最底部后当前的页码加1。
3、将页面传递给url的页码参数里再获取数据。
下面的代码主要实现以上三步:
服务端的URL地址:http://jsonplaceholder.typicode.com/posts/1/comments
1、index.html页面代码
                
    
        
        
        
        
        
        
    
            
        思路解析:
1、监听滚动条是否到了页面最底部。
2、到了页面最底部后当前的页码加1。
3、将页面传递给url的页码参数里再获取数据。
下面的代码主要实现以上三步:
服务端的URL地址:http://jsonplaceholder.typicode.com/posts/1/comments
1、index.html页面代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>上拉刷新</title>
    <style>
        .news-wrap{width:500px;height:auto;border:solid 1px #0AA6E8;padding-top:10px;padding-bottom:10px;}
        .news-wrap .list{width:100%;height:200px;border-bottom:1px solid #FF0000;line-height:50px;}
    </style>
</head>
<body>
<div id="news-wrap" class="news-wrap">
    <div class="list">新闻1</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻1</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
    <div class="list">新闻2</div>
</div>
<script src="jquery.js"></script>
<script src="uprefresh.js"></script>
<script>
    $(function(){
        var oNewsWrap=$("#news-wrap");
        oNewsWrap.uprefresh({"curPage":1,"maxPage":10,"offsetBottom":100},function(curpage){
            $.ajax({
                type:"get",
                url:"http://jsonplaceholder.typicode.com/posts/"+curpage+"/comments",
                success:function(data){
                    var str='';
                    for(i in data){
                        str+='<div class="list">'+data[i].name+'</div>'
                    }
                    oNewsWrap.append(str);
                }
            });
        });
    });
</script>
</body>
</html>
2、uprefresh.js代码
2、uprefresh.js代码
+function($){'use strict';
    var UpRefresh=function(opts,callback){
        if(opts instanceof Object) {
            this.opts = opts;
            this.iMaxPage=this.opts.maxPage;
            this.oWin=$(window);
            this.fnCallback=callback;
            this.oHtml=$("html");
            this.iOffsetBottom=this.opts.offsetBottom;
            this.iCurPage=this.opts.curPage;
            this.init();
        }
    };
    UpRefresh.prototype={
        init:function(){
            var _this=this;
            _this.eventScroll();
        },
        eventScroll:function(){
            var _this=this;
            _this.oWin.on("scroll",_this.scrollBottom());
        },
        scrollBottom:function(){//函数节流检测是否到了最底部
            var _this=this;
            var bScroll=true;
            return function(){
                if(!bScroll){
                    return;
                }
                bScroll=false;
                setTimeout(function(){
                    var iScrollHeight=$(document).innerHeight();//获取滚轮页面的高
                    var iScrollTop=_this.oWin.scrollTop();//获取滚动条到哪里的高
                    var iWinHeight=_this.oWin.height();//获取当前页面第一屏的高
                    if(iScrollHeight-(iWinHeight+iScrollTop)<=_this.iOffsetBottom){
                        if(_this.iCurPage<_this.iMaxPage) {
                            //console.log("scrollHeight:"+iScrollHeight+",winHeight:"+(parseInt(iScrollTop)+parseInt(iWinHeight)));
                            _this.iCurPage++;
                            //console.log(_this.iCurPage);
                            _this.fnCallback(_this.iCurPage);//利用回调函数将页码发送到index.html里面
                        }
                    }
                    bScroll=true;
                },600);
            }
        }
    };
    $.fn.uprefresh=function(opts,callback){
        return new UpRefresh(opts,callback);
    }
精品好课
    
